douke8473 2013-12-24 13:30
浏览 47
已采纳

php pdo选择查询不起作用[关闭]

i'm new to php PDO, i've made a query to select users in db and display them in a html table but i get no result, and there is no error message. print_r($connexion) returns PDO Object ( )

here is the code. thanks for your help!

connexion to 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;

query file //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条回答 默认 最新

  • duanqian6982 2013-12-24 14:07
    关注

    Hmm Here is your code:

    PHP Code [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 Code [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;
    }
    

    Some suggestions:

     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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同