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

标签 php mysql mysqli

我正在做一个项目,登录用户应该能够在主页上发布笔记,并且登录特定用户的笔记应该打印在新的笔记表单上方。 我为此编写了一个函数,其中 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/

相关文章:

php - 在 PHP 中从数组创建 .tar 文件

javascript - 在 PHP 中打印嵌套 Json

php - Mysql CPU占用率高(100%)

php - 将字符串常量与自动递增整数连接并存储在 mysql 数据库中

php - Mysqli查询函数插入重复行

php - W3Schools 试用编辑器

php - 数据映射器模式、异常和处理用户提供的数据

java - Hibernate更新主键

mysql - 数据字典 - 初学者

php - 分页:第二页上没有显示任何内容