Click here to Skip to main content
16,017,623 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This snippet works only when I make a connection with PDO but I want it with mysqli.

PHP
<?php

//fetch_comment.php

//$connect = new PDO('mysql:host=localhost;dbname=tbl_comment', 'root', '');

$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




PHP
$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
Posted
Updated 19-Aug-19 15:40pm
v5
Comments
Patrice T 16-Jul-19 18:17pm    
Think about searching the documentation.
OriginalGriff 17-Jul-19 1:57am    
Ooooo! RTFM Burn! :laugh:
Richard Deeming 18-Jul-19 11:45am    
$query = "
 SELECT * FROM tbl_comment WHERE parent_comment_id = '".$parent_id."'
 ";

Don't do that.

Whilst in this particular case you're probably safe, using string concatenation / interpolation to build queries will lead to SQL Injection[^].

PHP: SQL Injection - Manual[^]

1 solution

$result = mysqli_fetch_assoc($query);
or

$query = $mysqli->query("SELECT * FROM tbl_comment
WHERE parent_comment_id = '0'
ORDER BY comment_id DESC");
$result = $query->fetch_all(MYSQLI_ASSOC);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900