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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog