Hello all,
I have some questions about using mysqli, queries, and related memory management. The code here is just to clarify my questions, so don't dump on it for error checking, etc. I know that needs to be done :)
Suppose I have something like this:
@ $db = new mysqli($dbhost, $un, $ps, $dbname);
$query = "SELECT field1, field2 ".
"FROM table1 ".
"WHERE field1={$some_value}";
$results = $db->query($query);
while ($result = $results->fetch_object()) {
// do something with the results
}
$query = "SELECT field1, field2 ".
"FROM table2 ".
"WHERE field1={$some_value2}";
// 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 docs for mysqli::query has an example that uses close, even though close is not part of mysqli_result (see example 1 at http://ca3.php.net/manual/en/mysqli.query.php). And in contrast, my normal php reference text uses free (PHP and MySQL Web Development, Fourth Edition, Welling and Thomson).
Thanks in advance as always!