php - 使用 mysqli_fetch_all() 时未定义索引

标签 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 = '';

但我收到以下消息:

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

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

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

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

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

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

<?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);

?>

最佳答案

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

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

关于php - 使用 mysqli_fetch_all() 时未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57066401/

相关文章:

php - 是否有用于 Codeigniter 的 AJAX 库

php - 如何使用 PHP 列出 Google Cloud Storage 中存储桶中的所有文件?

php - 如何启用我的数据库连接 UTF8

mysql - 倒序时选择慢10倍

php - 使用变量时无法将数据添加到数据库字段中

php - 如何在一个查询中组合三个表并且我需要所有行?

php - 如何使用内部联接从两个日期之间的数据库中获取数据?

php - 发送POST请求: missing trailing slash in url triggers redirecting GET request

php - 在 foreach 循环中调用存储过程 - 仅首先执行

php - 从 MySQL 数据库输出数组...如何在 PHP 中删除方括号和双引号?