php pdo选择查询不起作用

时间:2013-12-24 13:30:42

标签: php mysql pdo

我是php PDO的新手,我已经做了一个查询来选择db中的用户并将它们显示在html表中但我没有得到结果,并且没有错误消息。 print_r($connexion)返回PDO Object ( )

这是代码。谢谢你的帮助!

连接到db //connexion.php

function connexion(){

try {
   $dns = 'mysql:host = localhost; dbname = pruf';
   $utilisateur = 'root';
   $motDePasse = '';
   $connexion = new PDO($dns, $utilisateur, $motDePasse, array(
   PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'
   ));
} 

catch (Exception $ex) {
   echo "connexion à Mysql impossible", $ex->getMessage();
   die('ok');
}

return $connexion;

查询文件//searchInvite.php

<!DOCTYPE HTML>
<head>
    <meta charset="utf-8"
</head>
<html>
<table border="1px">
    <tr>
        <td>Nom</td>
        <td>Prenom</td>
        <td>Fonction</td>
        <td>Titre</td>
        <td>Adresse</td>
</tr>

<?php
    require_once 'connexion.php';

   $connexion = connexion();
   print_r($connexion); 
   $query = "SELECT nom, prenom, fonction, titre_grade, adr_pro_voie FROM invite ORDER BY id LIMIT 0 , 30";

   $stmt = $connexion->prepare($query);


   $stmt->execute();

   while ($enregistrement = $stmt->fetchAll(PDO::FETCH_OBJ)){

?>

    <tr>
        <td><?php echo $enregistrement->nom; ?></td>
        <td><?php echo $enregistrement->prenom; ?></td>
        <td><?php echo $enregistrement->fonction; ?></td>
        <td><?php echo $enregistrement->titre_grade ?></td>
        <td><?php echo $enregistrement->adr_pro_voie; ?></td>
    </tr>


<?php 
}

?>
 </table>

1 个答案:

答案 0 :(得分:0)

嗯这是你的代码:

PHP代码[index.php]:

<!DOCTYPE HTML>
<head>
<meta charset="utf-8" </head>
<html>
<body>
<table border="1px">
    <tr>
        <td>Nom</td>
        <td>Prenom</td>
        <td>Fonction</td>
    </tr>
<?php
require_once 'connection.php';
$DBH = connection();
# creating the statement
$STH = $DBH->prepare("SELECT nom, prenom, fonction, titre_grade, adr_pro_voie FROM invite ORDER BY id LIMIT 0 , 30");
# setting the fetch mode  
$STH->setFetchMode(PDO::FETCH_OBJ);
# run the query
if ( $STH->execute()){
# showing the results  
while($enregistrement = $STH->fetch()) {  
?>
    <tr>
        <td><?php echo $enregistrement->nom; ?></td>
        <td><?php echo $enregistrement->prenom; ?></td>
        <td><?php echo $enregistrement->fonction; ?></td>
        <td><?php echo $enregistrement->titre_grade ?></td>
        <td><?php echo $enregistrement->adr_pro_voie; ?></td>
    </tr>

<?php }
}else {
    // if not run pdo statement send error
    $error = $STH->errorInfo();
    echo $error[2];
} ?>

</table>
</body>
</html>

PHP代码[connection.php]

function connection() {
    // try catch block start
    try {
        // use native pdo class and connect
        $DBH = new PDO("mysql:host=localhost;dbname=pruf", 'root', '', array(
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"
        )); 
    }
    catch (PDOException $e) {
        // get pdo error and pass on error method
        die("ERROR in establish connection: " . $e->getMessage());
    }
    return $DBH;
}

一些建议:

 1. Check your DB name
 2. Check your user name
 3. Check your password
 4. Check your server
 5. Check if user privilege access database
 6. Check table name, try to dry run query.