This snippet works only when I make a connection with PDO but I want it with mysqli.
<?php
$connect = mysqli_connect('localhost','root','','tbl_comment');
$query = "
SELECT * FROM tbl_comment
WHERE parent_comment_id = '0'
ORDER BY comment_id DESC
";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
$output .= '
<div class="panel panel-default">
<div class="panel-heading">By '.$row["comment_sender_name"].' on '.$row["date"].'</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 '.$row["comment_sender_name"].' on '.$row["date"].'</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;
}
?>
What I have tried:
I tried mysqli_fetch_all() but I am getting error messages :
Notice: Undefined index: comment_sender_name in C:\xampp\htdocs\tbl_comment\fetch_comment.php on line 46
Notice: Undefined index: date in C:\xampp\htdocs\tbl_comment\fetch_comment.php on line 46
Notice: Undefined index: comment in C:\xampp\htdocs\tbl_comment\fetch_comment.php on line 47
Notice: Undefined index: comment_id in C:\xampp\htdocs\tbl_comment\fetch_comment.php on line 48
Notice: Undefined index: comment_id in C:\xampp\htdocs\tbl_comment\fetch_comment.php on line 51
$statement = $connect ->prepare("SELECT * FROM tbl_comment
WHERE parent_comment_id = '0'
ORDER BY comment_id DESC");
$statement->execute();
$resultSet = $statement->get_result();
$result = $resultSet->fetch_all();
$output = '';
.....
$statement = $connect ->prepare("
SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
");
$statement->execute();
$resultSet = $statement->get_result();
$result = $resultSet->fetch_all();
$count = $statement->num_rows();
$output = '';
Update: I also udated the $reslut.
$result = $resultSet->fetch_all(MYSQLI_ASSOC);
And now the first query works but the second
(SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id.") doesn't