PHP: mysqli_result::free - Manual (original) (raw)
mysqli_result::close
mysqli_result::free_result
mysqli_free_result
(PHP 5, PHP 7, PHP 8)
mysqli_result::free -- mysqli_result::close -- mysqli_result::free_result -- mysqli_free_result — Frees the memory associated with a result
Description
Object-oriented style
public mysqli_result::free(): void
public mysqli_result::close(): void
public mysqli_result::free_result(): void
Return Values
No value is returned.
See Also
- mysqli_query() - Performs a query on the database
- mysqli_stmt_get_result() - Gets a result set from a prepared statement as a mysqli_result object
- mysqli_store_result() - Transfers a result set from the last query
- mysqli_use_result() - Initiate a result set retrieval
Found A Problem?
jack_action100 at hotmail dot example dot com ¶
6 years ago
If you are STILL getting this error, even after freeing your results:
Internal SQL Bug: 2014, Commands out of sync; you can't run this command now
You may have a stored procedure in your query. A procedure can return more than one result set, and it will always return one extra empty result set that carries some meta information on the procedure call itself, especially error information. ( source: https://bugs.mysql.com/bug.php?id=71044 )
While calling single procedures, with one SELECT in them, using mysqli->query("CALL `stored_procedure`();"), I had to do the following to make it work between two calls:
<?php
$result->free();
$mysqli->next_result();
?>
It has no negative impact if you are not calling a stored procedure.polygon dot co dot in at gmail dot com ¶
4 years ago
We should free the mysql results using mysqli_free_result respectively or else this will consume your server RAM resource.
This is demonstrated as below
<?php
$link = mysqli_connect('Hostname','Username','Password','Database');
echo '<br/>Memory Usage Before Query = '.memory_get_usage(false); // 449464 bytes
<span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>r</mi><mi>e</mi><mi>s</mi><mi>u</mi><mi>l</mi><mi>t</mi><mi>R</mi><mi>e</mi><mi>s</mi><mi>o</mi><mi>u</mi><mi>r</mi><mi>c</mi><mi>e</mi><mo>=</mo><mi>m</mi><mi>y</mi><mi>s</mi><mi>q</mi><mi>l</mi><msub><mi>i</mi><mi>q</mi></msub><mi>u</mi><mi>e</mi><mi>r</mi><mi>y</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">resultResource = mysqli_query(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">res</span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.00773em;">ltR</span><span class="mord mathnormal">eso</span><span class="mord mathnormal">u</span><span class="mord mathnormal">rce</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1.0361em;vertical-align:-0.2861em;"></span><span class="mord mathnormal">m</span><span class="mord mathnormal">ys</span><span class="mord mathnormal" style="margin-right:0.01968em;">ql</span><span class="mord"><span class="mord mathnormal">i</span><span class="msupsub"><span class="vlist-t vlist-t2"><span class="vlist-r"><span class="vlist" style="height:0.1514em;"><span style="top:-2.55em;margin-left:0em;margin-right:0.05em;"><span class="pstrut" style="height:2.7em;"></span><span class="sizing reset-size6 size3 mtight"><span class="mord mathnormal mtight" style="margin-right:0.03588em;">q</span></span></span></span><span class="vlist-s"></span></span><span class="vlist-r"><span class="vlist" style="height:0.2861em;"><span></span></span></span></span></span></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.03588em;">ery</span><span class="mopen">(</span></span></span></span>link, 'SELECT * FROM test');
echo '<br/>Memory Usage after Query = '.memory_get_usage(false); // 466528 bytes
$result = array();
while ($result[] = mysqli_fetch_assoc($resultResource)) {}
echo '<br/><br/>Memory Usage Before Free Result = '.memory_get_usage(false); // 474208 bytes
mysqli_free_result($resultResource);
echo '<br/>Memory Usage After Free Result = '.memory_get_usage(false); // 457336 bytes
?>
Output below.
Memory Usage Before Query = 449464
Memory Usage after Query = 466528
Memory Usage Before Free Result = 474208
Memory Usage After Free Result = 457336
So, One can observer there is memory usage after the query is executed. The results are returned by DB server to the web server instantaniously once the query execution is over. The results present on web server are then processed for fetching from the resource link on web server.
Also this is observed that there is lesser memory usage after using mysqli_free_result because the resources stored on web server for respective query are freed by providing respective resource link.
15 years ago
If you are getting this error:
Internal SQL Bug: 2014, Commands out of sync; you can't run this command now
Then you never called mysqli_result::free(), mysqli_result::free_result(), mysqli_result::close(), or mysqli_free_result() in your script, and must call it before executing another stored procedure.