mysqli_fetch_assoc只返回数组

时间:2016-07-27 07:04:46

标签: 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

6 个答案:

答案 0 :(得分:3)

你可以得到所有结果:

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

或使用while循环:

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

答案 1 :(得分:2)

1.您需要遍历result-set objectwhile()

2.将您的所有数据保存到array,然后返回array以获取所有记录

如下: -

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

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

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

    confirm_query($result);

    $final_data = array(); // create an array
    while($row = mysqli_fetch_assoc($result)){ // iterate over the result-set object to get all data
        $final_data[] = $row; //assign value to the array
    }
    return $final_data; // return array
  }
?>

<强> 现在: -

<?php
$notes_set = find_notes_by_id($userRow['id']);

print_r($notes_set) ; // print result to check array structure so that you can use it correctly in foreach
  foreach($notes_set as $note){
    echo $note['content'];
    echo "<br />";
  }
?>

答案 2 :(得分:1)

将数据保存到array。通过foreachfor循环使用它。

$content = [];
while($row = mysqli_fetch_assoc($result)){
   $content[] = $row['content'];
}
return $content;

答案 3 :(得分:1)

需要改变这样的功能

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 = array();
    while($result1 = mysqli_fetch_assoc($result)){
        //Array ( [content] => First! ) = it only shows the very first element
        $row[] = $result1;
    }
    return $row;
}

答案 4 :(得分:1)

你必须迭代。 问题在于此代码

$row = mysqli_fetch_assoc($result);
return $row;

将其更改为

$x = array();
while( $row = mysqli_fetch_assoc($result))
{
  $x[] = $row;
}
return $x;

答案 5 :(得分:1)

而是声明$ connection作为全局将$ connection变量传递给函数,这将是一种有效的方式并将结果作为数组

<?php    
            function find_notes_by_id($user_id,$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);


            confirm_query($result);

         $data = array(); // create an array
       while($row = mysqli_fetch_assoc($result)){ 
         $data[] = $row; 
    }
      return $data;
        }
        ?>

现在调用这个函数

 <?php
$notes_set = find_notes_by_id($userRow['id'],$connection);
  foreach($notes_set as $note){
    echo $note['content'];  
  }
?>