douchenbiao0916 2016-04-08 15:59
浏览 25
已采纳

无法使用PDO显示查询结果

I am trying to get the results from a query which should get multiple results and display them all on the page. However it is not displaying any of the content. My guess is a mistake in my syntax for me loop. But I am unsure.

//query to find comments about this map
$query = "
        SELECT 
           user_id,
           comment
         FROM map_comments
         WHERE
           map_id = :mapID
         ";  

//query parameters
$query_params = array(
  ':mapID' => $_SESSION['mapID']
);

try
{
    //execute query
    $statement = $db->prepare($query);
    $result = $statement->execute($query_params);
    //get all results
    $comments = $result->fetchAll;
    if($result === FALSE) 
    { 
      die(mysql_error()); // TODO: better error handling
    }
}
catch(PDOException $e)
{
    die("failed to find comments");
}

foreach($comments as &$comment)
      { 
        echo $comment;
      }
  • 写回答

1条回答 默认 最新

  • dongwo6477 2016-04-08 16:04
    关注

    You need parentheses after a function to call it.

    $comments = $result->fetchAll;
    

    should be:

    $comments = $statement->fetchAll();
    

    Also, the check for if ($result == FALSE) should be before this line. And you can't use mysql_error() if you're using PDO, you should use $statement->errorInfo(). Or you should enable PDO::ERRMODE_EXCEPTION on the connection, and the catch block will be invoked. You should then use $db->errorInfo() in the error message that it prints.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流