使用mysqli_fetch_all()时的未定义索引

时间:2019-07-16 22:47:45

标签: php mysql mysqli

仅当我与PDO建立连接但希望与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)
      {
.....
.....
...
    ?>

我尝试使用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 = '';

但是我收到以下消息:

注意:未定义的索引:第46行的C:\ xampp \ htdocs \ tbl_comment \ fetch_comment.php中的comment_sender_name

注意:未定义的索引:第46行的C:\ xampp \ htdocs \ tbl_comment \ fetch_comment.php中的日期

注意:未定义的索引:第47行的C:\ xampp \ htdocs \ tbl_comment \ fetch_comment.php中的注释

注意:未定义的索引:第48行的C:\ xampp \ htdocs \ tbl_comment \ fetch_comment.php中的comment_id

注意:未定义的索引:第51行的C:\ xampp \ htdocs \ tbl_comment \ fetch_comment.php中的comment_id

更新:感谢@Dharman,当我使用MYSQLI_ASSOC时,它向我显示注释(第一个MySQL语句)但不显示答复(第二个MySql语句)。它适用于PDO。我也有一个文件可以写注释,但是当我从PDO更改为mysqli时,它将在数据库中写入两次:

<?php

// add_comment.php

// $ connect =新的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);

?>

1 个答案:

答案 0 :(得分:1)

只需使用$result = $resultSet->fetch_all(MYSQLI_ASSOC);

默认情况下,fetch_all返回数字数组,但是您需要关联数组。将常量作为参数传递给fetch_all