duanpo1498 2018-03-29 15:04
浏览 63

PDO获取返回空值[重复]

This question already has an answer here:

I'm pretty new to PHP/SQL and I'm sure I'm making some simple mistake but I've searched for hours and cant figure out where it is. There's no errors being thrown when I test it.

Currently the below code just returns ---| as a result. to clarify the table im trying to get data from has 4 columns: name, room, in, out. it also has 6 rows of data that I can see is there in the database.

I'm trying to get it to return all of the data from the 6 rows that I have stored. Hopefully someone can show me the error of my ways

 <?php

        $db = new PDO('mysql:host=localhost;dbname=dbname','user','pass');

        $sql = "SELECT * FROM table";

        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

        $stmt = $db->prepare($sql);
        $stmt->execute();

        $error= $stmt->errorInfo();
        echo $error[2];

        while($row = $stmt->fetch(PDO::FETCH_ASSOC));
            {
            echo $row['name']. "-" . $row['room']. "-" . $row['in']. "-" .$row['out'].  "|";
            }
    ?>

UPDATE:

as suggested i removed the while loop and replaced it with

var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));

it looks like its able to fetch the results just fine, didnt even have to view the source to see the results but im unsure why the while loop isnt working then. heres the output after replacing the while loop (only showing the result for 1 row, but all 6 rows are identical).

array(4) {
  [0]=>
  array(4) {
    ["name"]=>
    string(4) "john"
    ["room"]=>
    string(4) "room"
    ["in"]=>
    string(1) "5"
    ["out"]=>
    string(2) "11"
  }
</div>
  • 写回答

1条回答 默认 最新

  • duanmuyao0463 2018-03-29 15:26
    关注

    the fact that it entered the while loop and gave you ---| proves that there was a result. but it's not rendered for some reason. could be an empty string. or could be that the results include <span style="display:none">foo</span> and that you're viewing the result in a web browser (which is actually rather common when working with PHP), whatever the reason is, replace the while loop with

    $stmt->execute();
    var_dump($stmt->fetchAll(PDO::FETCH_ASSOC));
    

    and view PHP's output in something that shows you the raw output (that means, do not view it in a browser that parses it as html. use the View-Source:URL method if you have to view it in a browser. or better yet, use curl from command line.), it should reveal what the actual results was.

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP