cyberRobot Posted September 11, 2014 Share Posted September 11, 2014 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? Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted September 11, 2014 Solution Share Posted September 11, 2014 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'); Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 11, 2014 Author Share Posted September 11, 2014 ...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! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted September 11, 2014 Share Posted September 11, 2014 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? It's destroyed after the variable has been overwritten with the new result object (hence the problem above). Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted September 11, 2014 Author Share Posted September 11, 2014 Got it; thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.