Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Couldn't fetch Mysqli_result

Tags:

php

mysqli

I've got this error

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

And would like to know what I've done wrong with the below code?

$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);
like image 240
dave Avatar asked Sep 02 '12 16:09

dave


People also ask

Why is mysqli_free_result () calling inside the fetch loop?

Straight away, it appears that you are calling mysqli_free_result () inside your fetch loop, so after the first loop iteration, your result resource has been closed and freed, and no more results will be available.

What is the use of fetch_array ()/mysqli_fetch_array () function?

The fetch_array () / mysqli_fetch_array () function fetches a result row as an associative array, a numeric array, or both. Note: Fieldnames returned from this function are case-sensitive.

Why is mysqli object not working in oop5?

" See: php.net/manual/en/language.oop5.decon.php Reason of the error is wrong initialization of the mysqli object. True construction would be like this:

Why does MySQL crash when I close the database connection?

Either way when the script ends the mysql connection is usually closed automatically I think it is because when you close the database connection the first time, you forget to do: And then when you try connecting to the database again, it craps out because it is still set to the closed connection.


1 Answers

Straight away, it appears that you are calling mysqli_free_result() inside your fetch loop, so after the first loop iteration, your result resource has been closed and freed, and no more results will be available.

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're calling mysqli_fetch_array() without specifying MYSQLI_ASSOC, and so you're getting both numeric and associative keys back. If you are using everything in your JSON, you don't need to do all those 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.
}
like image 107
Michael Berkowski Avatar answered Sep 25 '22 22:09

Michael Berkowski