sklrboy
sklrboy

Reputation: 451

mysqli_fetch_assoc returns ONLY the first element of the array

I'm working on a project, where the logged in user should be able to post notes on the homepage, and being logged in the certain user's notes should be printed above the new note form. I've written a function for that, where the mysqli_query recognizes all the 6 entries I have, but the mysqli_fetch_assoc prints only the first note out of 6. What could I do wrong? Here is my code:

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

Upvotes: 3

Views: 3609

Answers (6)

Sunil Pachlangia
Sunil Pachlangia

Reputation: 2071

Need to change the function like this

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;
}

Upvotes: 1

Nana Partykar
Nana Partykar

Reputation: 10548

Save data into an array. Use it through foreach or for loop.

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

Upvotes: 1

Athar
Athar

Reputation: 1063

You have to iterate over. Problem is with this code

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

Change that to

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

Upvotes: 1

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

Instead declaring $connection as global pass the $connection variable to the function which would be a efficient way and get the result as array

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

Now call the function like this

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

Upvotes: 1

Patchesoft
Patchesoft

Reputation: 317

You can get all results like so:

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

Or use a while loop:

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

Upvotes: 3

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72299

1.You need to iterate over your result-set object through while()

2.Save all your data to an array and then return that array to get all records

like below:-

<?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
  }
?>

Now:-

<?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 />";
  }
?>

Upvotes: 2

Related Questions