Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>When I assign the results of the second query to <code>$results</code>, what happens to the memory associated with the previous results?</p> </blockquote> <p>When you execute this:</p> <pre><code>$results = $db-&gt;query($query); </code></pre> <p>If there was something in <code>$results</code> before, this old content cannot be accessed anymore, as there is no reference left to it.</p> <p>In such a case, PHP will mark the old content of the variable as "<em>not needed anymore</em>" -- and it will be removed from memory when PHP needs some memory.</p> <p>This, at least, is true for general PHP variables; in the case of results from an SQL query, though, some data may be kept in memory on the driver-level -- over which PHP doesn't have much control.</p> <p><br></p> <blockquote> <p>Should I be freeing that result before assigning the new one?</p> </blockquote> <p>I never do that -- but, quoting the manual page of <a href="http://fr2.php.net/manual/en/mysqli-result.free.php" rel="noreferrer"><code>mysqli_result::free</code></a>:</p> <blockquote> <p>Note: You should always free your result with mysqli_free_result(), when your result object is not needed anymore</p> </blockquote> <p>It probably doesn't matter for a small script... And the only way to be sure would be to test, using <a href="http://fr2.php.net/memory_get_usage" rel="noreferrer"><code>memory_get_usage</code></a> before and after calling that method, to see whether there is a difference or not.</p> <p><br></p> <blockquote> <p>Related to 1, when I do clean up at the end, is cleaning up just the last results enough?</p> </blockquote> <p>When the scripts end:</p> <ul> <li>The connection to the database will be closed -- which means any memory that might be used by the driver should be freed</li> <li>All variables used by the PHP script will be destroyed -- which means the memory they were using should be freed.</li> </ul> <p>So, at the end of the script, there is probably really no need to free the resultset.</p> <p><br></p> <blockquote> <p>When I do try to clean up a result, should I be freeing it as above, should I be closing it, or both?</p> </blockquote> <p>If you close the connection to the database <em>(using <a href="http://fr2.php.net/manual/en/mysqli.close.php" rel="noreferrer"><code>mysqli::close</code></a> like you proposed)</em>, this will disconnect you from the database.</p> <p>Which means you'll have to re-connect if you want to do another query! Which is not good at all <em>(takes some time, resources, ... )</em></p> <p>Generally speaking, I would not close the connection to the database until I am really sure that I won't need it anymore -- which means I would not disconnect before the end of the script.</p> <p>And as "<em>end of the script</em>" means "<em>the connection will be closed</em>" even if you don't specify it; I almost never close the connection myself.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload