Failed to get Mysqli_result

I have this error

Warning : mysqli_fetch_array () [function.mysqli-fetch-array]: Failed to get mysqli_result in /home/fights7/public_html/include/load_more_home_posts.php on line 12

And I would like to know what I did wrong with the code below?

$articles_data = mysqli_query($mysqli,"SELECT * FROM streamdata WHERE streamitem_id < '$lastID' ORDER BY streamitem_id DESC LIMIT 10") or die(mysql_error()); while($articles_info = mysqli_fetch_array($articles_data)) { $json = array(); $json['streamitem_id'] = $articles_info['streamitem_id']; $json['streamitem_content'] = $articles_info['streamitem_content']; $json['streamitem_timestamp'] = $articles_info['streamitem_timestamp']; mysqli_free_result($articles_data); 
+4
source share
1 answer

Right, it seems that you are calling mysqli_free_result() inside the fetch loop, so after the first iteration of the loop, your result resource has been closed and freed, and there will be no more results.

 while($articles_info = mysqli_fetch_array($articles_data)) { $json = array(); $json['streamitem_id'] = $articles_info['streamitem_id']; $json['streamitem_content'] = $articles_info['streamitem_content']; $json['streamitem_timestamp'] = $articles_info['streamitem_timestamp']; // Don't do this! //mysqli_free_result($articles_data); } // If you need to, free it outside the loop mysqli_free_result($articles_data); 

I note that you are calling mysqli_fetch_array() without specifying MYSQLI_ASSOC , and therefore you are returning both numeric and associative keys. If you use everything in your JSON, you do not need to perform all these assignments if you use MYSQLI_ASSOC or mysqli_fetch_assoc() :

 while($articles_info = mysqli_fetch_assoc($articles_data)) { // No need for the $json array. Just use $articles_info directly // if you were going to json_encode() it. } 
+9
source

All Articles