doucitan2544 2015-05-12 21:39
浏览 37
已采纳

PDO仅提取单行

I am trying to fetch all rows with the Id that is taken from getting the cat_id:

<?php require_once '../db_con.php'; 

if(!empty($_GET['cat_id'])){
    $doc = intval($_GET['cat_id']);
try{
    $results = $dbh->prepare('SELECT * FROM cat_list WHERE cat_id = ?');
    $results->bindParam(1, $doc);
    $results->execute();

    } catch(Exception $e) {
    echo $e->getMessage();
    die();
    }

    $doc = $results->fetch(PDO::FETCH_ASSOC);    
    if($doc == FALSE){

        echo '<div class="container">';
        echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
        echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
        echo '<a href="userList.php"  style="margin: 40px auto; display: block; text-align: center;">Get me outta here!</a>';
        echo'</div>';
        die();
    }
}

?>

If I just use:

$doc = $results->fetch(PDO::FETCH_ASSOC);  

It fetches a single row as expcted

However if I use:

$doc = $results->fetchAll(PDO::FETCH_ASSOC);  

I get the following error?

Notice: Undefined index: doc_id in /Applications/MAMP/htdocs/dashboardr v3.1.7/catView.php on line 126

Notice: Undefined index: doc_title in /Applications/MAMP/htdocs/dashboardr v3.1.7/catView.php on line 126

But if I var_dump($doc);

It does return the values:

array(2) { [0]=> array(3) { ["doc_title"]=> string(5) "dsfsd" ["cat_no"]=> string(1) "4" ["doc_id"]=> string(2) "72" } [1]=> array(3) { ["doc_title"]=> string(14) "Adams Test Doc" ["cat_no"]=> string(1) "4" ["doc_id"]=> string(3) "120" } } 

I am really confused?

UPDATE

<?php include 'header.php'; ?>

<?php require_once '../db_con.php'; 

if(!empty($_GET['cat_id'])){
    $cat = intval($_GET['cat_id']);
try{
    $results = $dbh->prepare("SELECT doc_list.doc_title, doc_list.cat_no, doc_id FROM doc_list WHERE cat_no = ?");
    $results->bindParam(1, $cat);
    $results->execute();

    } catch(Exception $e) {
    echo $e->getMessage();
    die();
    }

    $doc = $results->fetch(PDO::FETCH_ASSOC);    
    if($doc == FALSE){

        echo '<div class="container">';
        echo "<img src='../img/404.jpg' style='margin: 40px auto; display: block;' />";
        echo "<h1 style='margin: 40px auto; display: block; text-align: center;' />Oh Crumbs! You upset the bubba!</h1>";
        echo '<a href="userList.php"  style="margin: 40px auto; display: block; text-align: center;">Get me outta here!</a>';
        echo'</div>';
        die();
    }
}

?>

<h3 class="subTitle"><i class="fa fa-file-text"></i> </span>Document List</h3>  

   <p><?php 

   var_dump($doc);

   echo '<a href="docView.php?doc_id='.$doc["doc_id"].'">'.$doc['doc_title'].'</a>' ?></p>


   I WANT ALL MY RESULTS LISTED HERE

</div>
</div>
  • 写回答

1条回答 默认 最新

  • duankuai6991 2015-05-12 21:43
    关注

    Because with fetch() you get always one row, so you have to put it into a loop like below to iterate through all rows:

    while($doc = $results->fetch(PDO::FETCH_ASSOC)) {
        //your code
    }
    

    And with fetchAll() you get all rows at once into one single array, so you have to iterate through the array e.g.

    $doc = $results->fetchAll(PDO::FETCH_ASSOC);  
    foreach($doc as $v) {
        //your code
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装