gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:38:55 25 4
gpt4 key购买 nike

此代码片段仅在我与 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com