1

I get this error:

Warning: mysqli_free_result() expects parameter 1 to be mysqli_result, boolean given in

In my code, I have:

// Perform it
$result = mysqli_query($connection, $query);

if ($result) {
  // Success
  echo "<h1>Uploaded results to database</h1>";
} else {
  echo "FAILURE: Couldn't perform query on database: " . mysqli_error($connection);
}


// Release
mysqli_free_result($result);

Most other solutions I have read say it is because the query might have failed, but that is not the case, because I test the returned data from the query to see if it exists, and it does, so the <h1> tag gets executed. How come I can't release it then? I thought I should always released returned data when I was done working with it?

Dharman
  • 26,923
  • 21
  • 73
  • 125
Seerex
  • 581
  • 1
  • 9
  • 20
  • 4
    From the manual: "Returns **FALSE** on failure. For successful `SELECT`, `SHOW`, `DESCRIBE` or `EXPLAIN` queries `mysqli_query()` will return a `mysqli_result` object. For **other successful queries** `mysqli_query()` will return **TRUE**" - my guess is that you're using an `UPDATE` query - and therefore you get a **TRUE** (boolean) return. You can only use `mysqli_free_result` on those queries which return a `mysqli_result` object – naththedeveloper Nov 08 '13 at 11:20
  • Post your query as well.. – Joke_Sense10 Nov 08 '13 at 11:21
  • Does this answer your question? [mysqli\_fetch\_assoc() expects parameter / Call to a member function bind\_param() errors. How to get the actual mysql error and fix it?](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param) – Dharman Mar 16 '20 at 00:25

3 Answers3

3

You should have a look at the manual.

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE"

My guess is that you're using an UPDATE query - and therefore you get a TRUE (boolean) return. You can only use mysqli_free_result on those queries which return a mysqli_result object.

To re-iterate, if you use a SELECT, SHOW, DESCRIBE or EXPLAIN query and the query runs successfully, you can make use of mysqli_free_result.

If you use anything other than the 4 mentioned above (like INSERT or UPDATE) you can't make use of mysqli_free_result because the result will always be TRUE (if successful) or FALSE (if failed).

naththedeveloper
  • 4,234
  • 6
  • 35
  • 43
  • I found this seems to allow the best of both worlds… `if (gettype($result)==="object") mysqli_free_result($result);` – JoLoCo Oct 23 '14 at 00:15
0

This happens when error is generated while executing query.
So, $result will have boolean value false.
You should try to release inside if clause

For more details, you can refer "Procedural style" example at official documentation

Bhavik Shah
  • 2,250
  • 1
  • 16
  • 32
0

Use This: $result->close();

Ferhat KOÇER
  • 3,394
  • 23
  • 25
  • Answers should should consist more than just plain code. The author of the question states clearly that he has problems in understanding the issue he describes. – hugo der hungrige Jan 07 '15 at 14:09