0

This snippet works only when I make a connection with PDO but I want it with mysqli.-->link

    <?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 <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)
      {
.....
.....
...
    ?>

I tried to use mysqli fetch_all

$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 = '';

but I am getting this 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

Update: Thanks to @Dharman when I use the MYSQLI_ASSOC it displays me the comments(first MySQL statement) but not the replies (second MySql statement).It worked on PDO. I also have a file to write a comment but when I change from PDO to mysqli it writes it two times in the database:

<?php

//add_comment.php

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

$connect=mysqli_connect('localhost','root','','tbl_comment');

$error = '';
$comment_name = '';
$comment_content = '';

if(empty($_POST["comment_name"]))
{
 $error .= '<p class="text-danger">Name is required</p>';
}
else
{
 $comment_name = $_POST["comment_name"];
}

if(empty($_POST["comment_content"]))
{
 $error .= '<p class="text-danger">Comment is required</p>';
}
else
{
 $comment_content = $_POST["comment_content"];
}

if($error == '')
{
 $query = "
 INSERT INTO tbl_comment 
 (parent_comment_id, comment, comment_sender_name) 
 VALUES (:parent_comment_id, :comment, :comment_sender_name)
 ";
 $statement = $connect->prepare($query);
 $statement->execute(
  array(
   ':parent_comment_id' => $_POST["comment_id"],
   ':comment'    => $comment_content,
   ':comment_sender_name' => $comment_name
  )
 );
 $error = '<label class="text-success">Comment Added</label>';
}

$data = array(
 'error'  => $error
);

echo json_encode($data);

?>
2
  • Can you post output of var_dump($result);? Commented Jul 16, 2019 at 22:51
  • @Dharman, yeah, It outputs the array. i.imgur.com/v9BqwD8.png Commented Jul 16, 2019 at 22:58

1 Answer 1

1

Just use $result = $resultSet->fetch_all(MYSQLI_ASSOC);

By default fetch_all returns numerical array, but you want an associative array. Pass the constant as an argument to fetch_all

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.