I have a habit of keeping my variable usage to a bare minimum. So I’m wondering if there is any advantage to be gained by the following:
$query = $mysqli->query('SELECT * FROM `people` ORDER BY `name` ASC LIMIT 0,30'); // Example 1 $query = $query->fetch_assoc(); // Example 2 $query_r = $query->fetch_assoc(); $query->free();
Now if I’m right Example 1 should be more efficient as $query is unset when I reassign it which should free any memory associated with it. However there is a method (MySQLi_Result::free()) which frees associated memory – is this the same thing?
If I don’t call ::free() to free any memory associated with the result but unset it by reassigning the variable am I doing the same thing? I don’t know how to log this sort of thing – does anyone have some ideas?
The manual seems to suggest that you should still be using
free()to release the memory. I believe the reasoning is thatfree()is freeing the memory in MySQL, not in PHP. Since PHP can’t garbage-collect for MySQL, you need to callfree().