Jump to content

Freeing MySQLi Objects


cyberRobot
Go to solution Solved by Jacques1,

Recommended Posts

I have gotten into the habit of freeing MySQLi result objects once they are no longer needed. However, I'm curious about the cases where multiple MySQLi objects are being used.

 

If I use the same variable to store the result objects, do I still need to call the free() method for each object? Or is an object automatically freed once the variable is overwritten with the new object?

Link to comment
Share on other sites

  • Solution

The results are automatically freed upon destruction of the object. And if your variable is the only reference to the object, then, yes, overwriting it will eventually destroy the object.

 

Note, however, that the old object still exists when you create your new query. That's a problem if you use an unbuffered result and haven't fetched all rows:

$stmt = $mysqli->query('SELECT 1', MYSQLI_USE_RESULT);
$stmt = $mysqli->query('SELECT 1');

This gives you an “Command out of sync” error. If you explicitly free the result, there's no such problem:

$stmt = $mysqli->query('SELECT 1', MYSQLI_USE_RESULT);
$stmt->free();
$stmt = $mysqli->query('SELECT 1');
Link to comment
Share on other sites

...overwriting it will eventually destroy the object.

 

I assume that "eventually destory" means that it will be destroyed as soon as the next query executes and the variable is overwritten...is that correct?

 

 

Note, however, that the old object still exists when you create your new query. That's a problem if you use an unbuffered result and...

 

I have yet to use the MYSQLI_USE_RESULT flag, but I appreciate the warning. :)

 

 

 

Thanks for your help Jacques1!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.