<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/mysqlinfo.concepts.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'mysqlinfo.concepts.buffering.php',
    1 => 'Buffered and Unbuffered queries',
    2 => 'Buffered and Unbuffered queries',
  ),
  'up' => 
  array (
    0 => 'mysqlinfo.concepts.php',
    1 => 'Concepts',
  ),
  'prev' => 
  array (
    0 => 'mysqlinfo.concepts.php',
    1 => 'Concepts',
  ),
  'next' => 
  array (
    0 => 'mysqlinfo.concepts.charset.php',
    1 => 'Character sets',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/mysqlinfo/concepts.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="mysqlinfo.concepts.buffering" class="section">
  <h2 class="title">Buffered and Unbuffered queries</h2>

  <p class="simpara">
   Queries are using the buffered mode by default. This means that query results are
   immediately transferred from the MySQL Server to PHP and then are kept  in the memory
   of the PHP process. This allows additional operations like counting the
   number of rows, and moving (seeking) the current result pointer. It also allows
   issuing further queries on the same connection while working on the result set.
   The downside of the buffered mode is that larger result sets might require
   quite a lot memory. The memory will be kept occupied till all references to the
   result set are unset or the result set was explicitly freed, which will automatically
   happen during request end at the latest. The terminology &quot;store result&quot; is also used
   for buffered mode, as the whole result set is stored at once.
  </p>

  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    When using libmysqlclient as library PHP&#039;s memory limit won&#039;t count the memory used
    for result sets unless the data is fetched into PHP variables. With mysqlnd
    the memory accounted for will include the full result set.
   </span>
  </p></blockquote>

  <p class="simpara">
   Unbuffered MySQL queries execute the query and then wait
   for the data from the MySQL server to be fetched. This uses less memory
   on the PHP-side, but can increase the load on the server. Unless the full result set was
   fetched from the server no further queries can be sent over the same connection. Unbuffered
   queries can also be referred to as &quot;use result&quot;. Once all rows in the result set
   are fetched, the result set is gone and it cannot be iterated again.
  </p>

  <p class="simpara">
   Following these characteristics, unbuffered queries should be used only
   when a large result set is expected that will be processed sequentially.
   Unbuffered queries contain a number of pitfalls that makes it more
   difficult to use them, e.g. the number of rows in the result set is unknown
   until the last row is fetched.
   Buffered queries are the easier and more flexible way to process result sets.
  </p>

  

  <p class="simpara">
   Because buffered queries are the default, the examples below will demonstrate how to
   execute unbuffered queries with each API.
  </p>

  <div class="example" id="example-1">
   <p><strong>Example #1 Unbuffered query example: mysqli</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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 /></span><span style="color: #0000BB">$unbufferedResult </span><span style="color: #007700">= </span><span style="color: #0000BB">$mysqli</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT Name FROM City"</span><span style="color: #007700">, </span><span style="color: #0000BB">MYSQLI_USE_RESULT</span><span style="color: #007700">);<br /><br />foreach (</span><span style="color: #0000BB">$unbufferedResult </span><span style="color: #007700">as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'Name'</span><span style="color: #007700">] . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>

  <div class="example" id="example-2">
   <p><strong>Example #2 Unbuffered query example: pdo_mysql</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$pdo </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysql:host=localhost;dbname=world"</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">);<br /></span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">MYSQL_ATTR_USE_BUFFERED_QUERY</span><span style="color: #007700">, </span><span style="color: #0000BB">false</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$unbufferedResult </span><span style="color: #007700">= </span><span style="color: #0000BB">$pdo</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT Name FROM City"</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$unbufferedResult </span><span style="color: #007700">as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br />    echo </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'Name'</span><span style="color: #007700">] . </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div><?php manual_footer($setup); ?>