Reputation: 33
The information are sending frolm database but this is not displaing in my website and I get this error:
Fatal error: Uncaught Error: Call to undefined method PDO::fetchAll() in /storage/ssd1/525/6600525/public_html/ajax_message_forms/fetch_comment.php:14 Stack trace: #0 {main} thrown in /storage/ssd1/525/6600525/public_html/ajax_message_forms/fetch_comment.php on line 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;
}
?>
Upvotes: 0
Views: 12041
Reputation: 15247
fetchAll()
is a method of an object PDOStatement
, which is created and returned when you are calling $statement = $connect->prepare($query);
This is the fix you need to apply to your code
$statement->execute();
$result = $statement->fetchAll();
// ^--------^-----------------This instead of $connect
This query "SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'"
is vulnerable to SQL injections Consider using prepared statements.
In example :
$query = "SELECT * FROM tbl_comment WHERE parent_comment_id = :parent_id";
$statement = $connect->prepare($query);
$statement->bindParam(':parent_id', $parent_id);
$statement->execute();
Upvotes: 5