I have some questions about using MySQLi queries, and related memory management.
Suppose I have something like this:
$db = new mysqli($dbhost, $un, $ps, $dbname);
$query = "SELECT field1, field2 FROM table1 ";
$results = $db->query($query);
while ($result = $results->fetch_object()) {
// Do something with the results
}
$query = "SELECT field1, field2 FROM table2 ";
// question 1
$results = $db->query($query);
while ($result = $results->fetch_object()) {
// Do something with the second set of results
}
// Tidy up, question 2
if ($results) {
$results->free();
}
if ($db) {
$db->close();
}
// Question 3, a general one
So, based on the comments in the code above, here are my questions:
When I assign the results of the second query to
$results
, what happens to the memory associated with the previous results? Should I be freeing that result before assigning the new one?Related to 1, when I do clean up at the end, is cleaning up just the last results enough?
When I do try to clean up a result, should I be freeing it as above, should I be closing it, or both?
I ask question 3 because the PHP documentation for mysqli::query
has an example that uses close, even though close is not part of mysqli_result
(see example 1 in the link above). And in contrast, my normal PHP reference text uses free
(PHP and MySQL Web Development, Fourth Edition, Welling and Thomson).