gpt4 book ai didi

php - mysqli_fetch_assoc 只返回数组的第一个元素

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

我正在做一个项目,登录用户应该能够在主页上发布笔记,并且登录特定用户的笔记应该打印在新的笔记表单上方。我为此编写了一个函数,其中 mysqli_query 识别我拥有的所有 6 个条目,但 mysqli_fetch_assoc 仅打印 6 个中的第一个音符。我能做什么错误的?这是我的代码:

<?php    
function find_notes_by_id($user_id) {
global $connection;

$safe_user_id = mysqli_real_escape_string($connection, $user_id);

$query = 'SELECT content ';
$query .= 'FROM notes ';
$query .= 'WHERE user_id = '.$safe_user_id;
$result = mysqli_query($connection, $query);

//mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 6 [type] => 0 )

confirm_query($result);

$row = mysqli_fetch_assoc($result);
//Array ( [content] => First! ) = it only shows the very first element
return $row;
}
?>

<?php
$notes_set = find_notes_by_id($userRow['id']);
foreach($notes_set as $note){
echo $note;
echo "<br />";
}
?>

enter image description here

最佳答案

你可以像这样得到所有的结果:

$arr = $result->fetch_all(MYSQLI_ASSOC);

或者使用 while 循环:

while($row = $result->fetch_assoc()){
echo $row['content'] . '<br />';
}

关于php - mysqli_fetch_assoc 只返回数组的第一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605969/

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