<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Initiate a result set retrieval</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqli.thread-safe.html">mysqli::thread_safe</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqli.warning-count.html">mysqli::$warning_count</a></div> <div class="up"><a href="class.mysqli.html">mysqli</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="mysqli.use-result" class="refentry"> <div class="refnamediv"> <h1 class="refname">mysqli::use_result</h1> <h1 class="refname">mysqli_use_result</h1> <p class="verinfo">(PHP 5)</p><p class="refpurpose"><span class="refname">mysqli::use_result</span> -- <span class="refname">mysqli_use_result</span> — <span class="dc-title">Initiate a result set retrieval</span></p> </div> <div class="refsect1 description" id="refsect1-mysqli.use-result-description"> <h3 class="title">Description</h3> <p class="para">Object oriented style</p> <div class="methodsynopsis dc-description"> <span class="type"><a href="class.mysqli-result.html" class="type mysqli_result">mysqli_result</a></span> <span class="methodname"><strong>mysqli::use_result</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment">Procedural style</p> <div class="methodsynopsis dc-description"> <span class="type"><a href="class.mysqli-result.html" class="type mysqli_result">mysqli_result</a></span> <span class="methodname"><strong>mysqli_use_result</strong></span> ( <span class="methodparam"><span class="type"><a href="class.mysqli.html" class="type mysqli">mysqli</a></span> <code class="parameter">$link</code></span> )</div> <p class="para rdfs-comment"> Used to initiate the retrieval of a result set from the last query executed using the <span class="function"><a href="mysqli.real-query.html" class="function">mysqli_real_query()</a></span> function on the database connection. </p> <p class="para"> Either this or the <span class="function"><a href="mysqli.store-result.html" class="function">mysqli_store_result()</a></span> function must be called before the results of a query can be retrieved, and one or the other must be called to prevent the next query on that database connection from failing. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <span class="function"><strong>mysqli_use_result()</strong></span> function does not transfer the entire result set from the database and hence cannot be used functions such as <span class="function"><a href="mysqli-result.data-seek.html" class="function">mysqli_data_seek()</a></span> to move to a particular row within the set. To use this functionality, the result set must be stored using <span class="function"><a href="mysqli.store-result.html" class="function">mysqli_store_result()</a></span>. One should not use <span class="function"><strong>mysqli_use_result()</strong></span> if a lot of processing on the client side is performed, since this will tie up the server and prevent other threads from updating any tables from which the data is being fetched. </p> </p></blockquote> </div> <div class="refsect1 returnvalues" id="refsect1-mysqli.use-result-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an unbuffered result object or <strong><code>FALSE</code></strong> if an error occurred. </p> </div> <div class="refsect1 examples" id="refsect1-mysqli.use-result-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-1688"> <p><strong>Example #1 <span class="methodname"><strong>mysqli::use_result()</strong></span> example</strong></p> <div class="example-contents"><p>Object oriented style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$mysqli </span><span style="color: #007700">= new </span><span style="color: #0000BB">mysqli</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* check connection */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connect failed: %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br /> exit();<br />}<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT CURRENT_USER();"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">.= </span><span style="color: #DD0000">"SELECT Name FROM City ORDER BY ID LIMIT 20, 5"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* execute multi query */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">multi_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$query</span><span style="color: #007700">)) {<br /> do {<br /> </span><span style="color: #FF8000">/* store first result set */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">use_result</span><span style="color: #007700">()) {<br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">fetch_row</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /> }<br /> </span><span style="color: #0000BB">$result</span><span style="color: #007700">-></span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /> }<br /> </span><span style="color: #FF8000">/* print divider */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">more_results</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----------------\n"</span><span style="color: #007700">);<br /> }<br /> } while (</span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">next_result</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #FF8000">/* close connection */<br /></span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-></span><span style="color: #0000BB">close</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>Procedural style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$link </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_connect</span><span style="color: #007700">(</span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_user"</span><span style="color: #007700">, </span><span style="color: #DD0000">"my_password"</span><span style="color: #007700">, </span><span style="color: #DD0000">"world"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* check connection */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">mysqli_connect_errno</span><span style="color: #007700">()) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Connect failed: %s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">mysqli_connect_error</span><span style="color: #007700">());<br /> exit();<br />}<br /><br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT CURRENT_USER();"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$query </span><span style="color: #007700">.= </span><span style="color: #DD0000">"SELECT Name FROM City ORDER BY ID LIMIT 20, 5"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">/* execute multi query */<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">mysqli_multi_query</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">, </span><span style="color: #0000BB">$query</span><span style="color: #007700">)) {<br /> do {<br /> </span><span style="color: #FF8000">/* store first result set */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_use_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">)) {<br /> while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">mysqli_fetch_row</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"%s\n"</span><span style="color: #007700">, </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /> }<br /> </span><span style="color: #0000BB">mysqli_free_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #FF8000">/* print divider */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">mysqli_more_results</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">)) {<br /> </span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"-----------------\n"</span><span style="color: #007700">);<br /> }<br /> } while (</span><span style="color: #0000BB">mysqli_next_result</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #FF8000">/* close connection */<br /></span><span style="color: #0000BB">mysqli_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$link</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above examples will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> my_user@localhost ----------------- Amersfoort Maastricht Dordrecht Leiden Haarlemmermeer </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-mysqli.use-result-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="mysqli.real-query.html" class="function" rel="rdfs-seeAlso">mysqli_real_query()</a> - Execute an SQL query</span></li> <li class="member"> <span class="function"><a href="mysqli.store-result.html" class="function" rel="rdfs-seeAlso">mysqli_store_result()</a> - Transfers a result set from the last query</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="mysqli.thread-safe.html">mysqli::thread_safe</a></div> <div class="next" style="text-align: right; float: right;"><a href="mysqli.warning-count.html">mysqli::$warning_count</a></div> <div class="up"><a href="class.mysqli.html">mysqli</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>