调用未定义的方法PDO :: fetchAll()

时间:2019-01-27 14:19:35

标签: php

信息正在发送frolm数据库,但这并没有显示在我的网站上,并且出现此错误:

  

致命错误:未捕获错误:调用未定义的方法PDO :: fetchAll()   在   /storage/ssd1/525/6600525/public_html/ajax_message_forms/fetch_comment.php:14   堆栈跟踪:抛出#0 {main}   /storage/ssd1/525/6600525/public_html/ajax_message_forms/fetch_comment.php   在第14行

<?php

$connect = new PDO('mysql:host=localhost;dbname=xx' ,'xx' ,'xx');
$query = "
SELECT * FROM tbl_comment 
WHERE parent_comment_id = '0' 
ORDER BY comment_id DESC
";

$statement = $connect->prepare($query);

$statement->execute();

$result = $connect->fetchAll();
$output = '';
foreach($result as $row)
{
 $output .= '
 <div class="panel panel-default">
  <div class="panel-heading">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></div>
  <div class="panel-body">'.$row["comment"].'</div>
  <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
 </div>
 ';
 $output .= get_reply_comment($connect, $row["comment_id"]);
}

echo $output;

function get_reply_comment($connect, $parent_id = 0, $marginleft = 0)
{
 $query = "
 SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
 ";
 $output = '';
 $statement = $connect->prepare($query);
 $statement->execute();
 $result = $statement->fetchAll();
 $count = $statement->rowCount();
 if($parent_id == 0)
 {
  $marginleft = 0;
 }
 else
 {
  $marginleft = $marginleft + 48;
 }
 if($count > 0)
 {
  foreach($result as $row)
  {
   $output .= '
   <div class="panel panel-default" style="margin-left:'.$marginleft.'px">
    <div class="panel-heading">By <b>'.$row["comment_sender_name"].'</b> on <i>'.$row["date"].'</i></div>
    <div class="panel-body">'.$row["comment"].'</div>
    <div class="panel-footer" align="right"><button type="button" class="btn btn-default reply" id="'.$row["comment_id"].'">Reply</button></div>
   </div>
   ';
   $output .= get_reply_comment($connect, $row["comment_id"], $marginleft);
  }
 }
 return $output;
}

?>

1 个答案:

答案 0 :(得分:4)

fetchAll()是对象PDOStatement的方法,该对象在调用$statement = $connect->prepare($query);

时创建并返回

这是您需要应用到代码中的修复程序

$statement->execute();

$result = $statement->fetchAll();
//        ^--------^-----------------This instead of $connect

此查询"SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'"易受SQL injections的影响,请考虑使用prepared statements

例如:

$query = "SELECT * FROM tbl_comment WHERE parent_comment_id = :parent_id";
$statement = $connect->prepare($query);
$statement->bindParam(':parent_id', $parent_id);
$statement->execute();