<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdostatement.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'uk',
  ),
  'this' => 
  array (
    0 => 'pdostatement.fetch.php',
    1 => 'PDOStatement::fetch',
    2 => 'Fetches the next row from a result set',
  ),
  'up' => 
  array (
    0 => 'class.pdostatement.php',
    1 => 'PDOStatement',
  ),
  'prev' => 
  array (
    0 => 'pdostatement.execute.php',
    1 => 'PDOStatement::execute',
  ),
  'next' => 
  array (
    0 => 'pdostatement.fetchall.php',
    1 => 'PDOStatement::fetchAll',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo/pdostatement/fetch.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdostatement.fetch" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::fetch</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement::fetch</span> &mdash; <span class="dc-title">
   Fetches the next row from a result set
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.fetch-description">
  <h3 class="title">Опис</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">function</span> <span class="methodname"><strong>PDOStatement::fetch</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$mode</code><span class="initializer"> = <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-default">PDO::FETCH_DEFAULT</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$cursorOrientation</code><span class="initializer"> = <strong><code><a href="pdo.constants.php#pdo.constants.fetch-ori-next">PDO::FETCH_ORI_NEXT</a></code></strong></span></span>, <span class="methodparam"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span> <code class="parameter">$cursorOffset</code><span class="initializer"> = 0</span></span>): <span class="type"><a href="language.types.mixed.php" class="type mixed">mixed</a></span></div>


  <p class="simpara">
   Fetches a row from a result set associated with a
   <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span> instance.
   The <code class="parameter">mode</code> parameter determines how PDO returns the row.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.fetch-parameters">
  <h3 class="title">Параметри</h3>
  <dl>
   
    <dt><code class="parameter">mode</code></dt>
    <dd>
     <p class="para">
      Controls how the next row will be returned to the caller.
      This value must be one of the
      <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-default">PDO::FETCH_<span class="replaceable">*</span></a></code></strong> constants,
      defaulting to value of <strong><code><a href="pdo.constants.php#pdo.constants.attr-default-fetch-mode">PDO::ATTR_DEFAULT_FETCH_MODE</a></code></strong>
      (which defaults to <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-both">PDO::FETCH_BOTH</a></code></strong>).
      <ul class="itemizedlist">
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-assoc">PDO::FETCH_ASSOC</a></code></strong>: returns an array indexed by
         column name as returned in the result set.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-named">PDO::FETCH_NAMED</a></code></strong>: returns an array with the same
         form as <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-assoc">PDO::FETCH_ASSOC</a></code></strong>,
         except that if there are multiple columns with the same name,
         the value referred to by that key will be an array of all the values
         in the row that had that column name.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-num">PDO::FETCH_NUM</a></code></strong>: returns an array indexed by column
         number as returned in the result set, starting at column 0.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-both">PDO::FETCH_BOTH</a></code></strong>: returns an array indexed by
         both column name and 0-indexed column number as returned in the
         result set.
         This is effectively a combination of
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-num">PDO::FETCH_NUM</a></code></strong> and
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-assoc">PDO::FETCH_ASSOC</a></code></strong>.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-bound">PDO::FETCH_BOUND</a></code></strong>: returns <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong> and assigns
         the values of the columns in the result set to the PHP variables to
         which they were bound with the
         <span class="methodname"><a href="pdostatement.bindcolumn.php" class="methodname">PDOStatement::bindColumn()</a></span> method.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-obj">PDO::FETCH_OBJ</a></code></strong>: returns an instance of
         <span class="classname"><a href="class.stdclass.php" class="classname">stdClass</a></span> with property names that correspond
         to the column names returned in the result set.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-class">PDO::FETCH_CLASS</a></code></strong>: returns a new instance of the
         requested class. Unless the class was set with
         <span class="methodname"><a href="pdostatement.setfetchmode.php" class="methodname">PDOStatement::setFetchMode()</a></span>, this mode must be
         combined with <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-classtype">PDO::FETCH_CLASSTYPE</a></code></strong>, in which case
         the class to instantiate is determined by the value of the
         first column.
        </p>
        <p class="simpara">
         By default, the object is initialized by mapping the columns of
         the result set to properties in the class.
         This occurs <em>prior</em> to calling the constructor,
         allowing properties to be populated regardless of their visibility
         or whether they are marked as <code class="literal">readonly</code>, as long as
         the constructor does not initialize them itself.

         If a property does not exist in the class, the magic
         <a href="language.oop5.overloading.php#object.set" class="link">__set()</a>
         method will be invoked if it exists; otherwise, a dynamic public
         property will be created.
        </p>
        <p class="simpara">
         It is possible to change this behaviour by using the
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-props-late">PDO::FETCH_PROPS_LATE</a></code></strong> flag to call the
         constructor <em>before</em> the properties are populated.
        </p>
        <div class="caution"><strong class="caution">Застереження</strong>
         <p class="simpara">
          The column values are <em>not</em> passed as arguments
          to the constructor, regardless of whether
          <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-props-late">PDO::FETCH_PROPS_LATE</a></code></strong> is used. Only arguments given to
          <span class="methodname"><a href="pdostatement.setfetchmode.php" class="methodname">PDOStatement::setFetchMode()</a></span> are passed.
         </p>
        </div>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-into">PDO::FETCH_INTO</a></code></strong>: updates an existing instance
         of the requested class, mapping the columns of the result set to
         named properties in the class.
        </p>
       </li>
       <li class="listitem">
        <p class="simpara">
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-lazy">PDO::FETCH_LAZY</a></code></strong>: combines
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-both">PDO::FETCH_BOTH</a></code></strong> and
         <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-obj">PDO::FETCH_OBJ</a></code></strong>,
         and returns a <span class="classname"><a href="class.pdorow.php" class="classname">PDORow</a></span> object
         which creates the object property names as they are accessed.
        </p>
       </li>
      </ul>
     </p>
    </dd>
   
   
    <dt><code class="parameter">cursorOrientation</code></dt>
    <dd>
     <p class="simpara">
      For a <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span> object representing a
      scrollable cursor, this value determines which row will be returned
      to the caller. This value must be one of the
      <strong><code><a href="pdo.constants.php#pdo.constants.fetch-ori-next">PDO::FETCH_ORI_<span class="replaceable">*</span></a></code></strong>
      constants, defaulting to <strong><code><a href="pdo.constants.php#pdo.constants.fetch-ori-next">PDO::FETCH_ORI_NEXT</a></code></strong>.
      To request a scrollable cursor for the
      <span class="classname"><a href="class.pdostatement.php" class="classname">PDOStatement</a></span> object,
      the <strong><code><a href="pdo.constants.php#pdo.constants.attr-cursor">PDO::ATTR_CURSOR</a></code></strong> attribute must be set to
      <strong><code><a href="pdo.constants.php#pdo.constants.cursor-scroll">PDO::CURSOR_SCROLL</a></code></strong> when the SQL statement is
      prepared with <span class="methodname"><a href="pdo.prepare.php" class="methodname">PDO::prepare()</a></span>.
     </p>
    </dd>
   
   
    <dt><code class="parameter">cursorOffset</code></dt>
    <dd>
     <p class="simpara">
      If the value of the <code class="parameter">cursorOrientation</code> parameter
      is <strong><code><a href="pdo.constants.php#pdo.constants.fetch-ori-abs">PDO::FETCH_ORI_ABS</a></code></strong>, this value specifies the
      absolute number of the row in the result set that shall be fetched.
     </p>
     <p class="simpara">
      If the value of the <code class="parameter">cursorOrientation</code> parameter
      is <strong><code><a href="pdo.constants.php#pdo.constants.fetch-ori-rel">PDO::FETCH_ORI_REL</a></code></strong>, this value specifies the
      row to fetch relative to the cursor position before
      <span class="methodname"><strong>PDOStatement::fetch()</strong></span> was called.
     </p>
    </dd>
   
  </dl>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetch-returnvalues">
  <h3 class="title">Значення, що повертаються</h3>
  <p class="simpara">
   The return value of this function on success depends on the fetch type. In
   all cases, <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong> is returned on failure or if there are no more rows.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-pdostatement.fetch-errors">
  <h3 class="title">Помилки/виключення</h3>
  <p class="para">
Видає помилку рівня <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong>, якщо атрибут
<strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> дорівнює
<strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>.
</p>
<p class="para">
Викидає <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span>, якщо атрибут
<strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> дорівнює
<strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>.
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetch-examples">
  <h3 class="title">Приклади</h3>
  <div class="example" id="example-1">
   <p><strong>Приклад #1 Fetching rows using different fetch styles</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
$db </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'sqlite::memory:'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE TABLE fruit (name VARCHAR(100), colour VARCHAR(100))"</span><span style="color: #007700">);
</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO fruit (name, colour) VALUES
                             ('apple', 'red'),
                             ('banana', 'yellow'),
                             ('orange', 'orange'),
                             ('kiwi', 'green')"</span><span style="color: #007700">);
</span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT name, colour FROM fruit"</span><span style="color: #007700">);
</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();

</span><span style="color: #FF8000">/* Exercise PDOStatement::fetch styles */
</span><span style="color: #007700">echo </span><span style="color: #DD0000">"PDO::FETCH_ASSOC: "</span><span style="color: #007700">;
echo </span><span style="color: #DD0000">"Return next row as an array indexed by column name\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ASSOC</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

echo </span><span style="color: #DD0000">"PDO::FETCH_BOTH: "</span><span style="color: #007700">;
echo </span><span style="color: #DD0000">"Return next row as an array indexed by both column name and number\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_BOTH</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

echo </span><span style="color: #DD0000">"PDO::FETCH_LAZY: "</span><span style="color: #007700">;
echo </span><span style="color: #DD0000">"Return next row as a PDORow object with column names as properties\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_LAZY</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;

echo </span><span style="color: #DD0000">"PDO::FETCH_OBJ: "</span><span style="color: #007700">;
echo </span><span style="color: #DD0000">"Return next row as an stdClass object with column names as properties\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_OBJ</span><span style="color: #007700">);
</span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);
echo </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">PDO::FETCH_ASSOC: Return next row as an array indexed by column name
array(2) {
  [&quot;name&quot;]=&gt;
  string(5) &quot;apple&quot;
  [&quot;colour&quot;]=&gt;
  string(3) &quot;red&quot;
}

PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number
array(4) {
  [&quot;name&quot;]=&gt;
  string(6) &quot;banana&quot;
  [0]=&gt;
  string(6) &quot;banana&quot;
  [&quot;colour&quot;]=&gt;
  string(6) &quot;yellow&quot;
  [1]=&gt;
  string(6) &quot;yellow&quot;
}

PDO::FETCH_LAZY: Return next row as a PDORow object with column names as properties
object(PDORow)#3 (3) {
  [&quot;queryString&quot;]=&gt;
  string(30) &quot;SELECT name, colour FROM fruit&quot;
  [&quot;name&quot;]=&gt;
  string(6) &quot;orange&quot;
  [&quot;colour&quot;]=&gt;
  string(6) &quot;orange&quot;
}

PDO::FETCH_OBJ: Return next row as an stdClass object with column names as properties
object(stdClass)#4 (2) {
  [&quot;name&quot;]=&gt;
  string(4) &quot;kiwi&quot;
  [&quot;colour&quot;]=&gt;
  string(5) &quot;green&quot;
}</pre>
</div>
   </div>
  </div>
  <div class="example" id="example-2">
   <p><strong>Приклад #2 Fetching rows with a scrollable cursor</strong></p>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">function </span><span style="color: #0000BB">readDataForwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">) {
    </span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT hand, won, bet FROM mynumbers ORDER BY BET'</span><span style="color: #007700">;
    </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">, array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_SCROLL</span><span style="color: #007700">));
    </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();
    while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_NEXT</span><span style="color: #007700">)) {
        </span><span style="color: #0000BB">$data </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">] . </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        print </span><span style="color: #0000BB">$data</span><span style="color: #007700">;
    }
}
function </span><span style="color: #0000BB">readDataBackwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">) {
    </span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">'SELECT hand, won, bet FROM mynumbers ORDER BY bet'</span><span style="color: #007700">;
    </span><span style="color: #0000BB">$stmt </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">, array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_SCROLL</span><span style="color: #007700">));
    </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();
    </span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_LAST</span><span style="color: #007700">);
    do {
        </span><span style="color: #0000BB">$data </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">] . </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\t" </span><span style="color: #007700">. </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;
        print </span><span style="color: #0000BB">$data</span><span style="color: #007700">;
    } while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_NUM</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_PRIOR</span><span style="color: #007700">));
}

print </span><span style="color: #DD0000">"Reading forwards:\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">readDataForwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);

print </span><span style="color: #DD0000">"Reading backwards:\n"</span><span style="color: #007700">;
</span><span style="color: #0000BB">readDataBackwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад
виведе:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Reading forwards:
21    10    5
16    0     5
19    20    10

Reading backwards:
19    20    10
16    0     5
21    10    5</pre>
</div>
   </div>
  </div>

  <div class="example" id="example-3">
   <p><strong>Приклад #3 Construction order</strong></p>
   <div class="example-contents"><p>
    When objects are fetched via <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-class">PDO::FETCH_CLASS</a></code></strong>, the object
    properties are assigned first, and then the constructor of the class is
    invoked. However, when <strong><code><a href="pdo.constants.fetch-modes.php#pdo.constants.fetch-props-late">PDO::FETCH_PROPS_LATE</a></code></strong> is also given,
    this order is reversed, i.e. first the constructor is called, and
    afterwards the properties are assigned.
   </p></div>
   <div class="example-contents">
<div class="phpcode"><pre><code style="color: #000000"><span style="color: #0000BB">&lt;?php
</span><span style="color: #007700">class </span><span style="color: #0000BB">Person
</span><span style="color: #007700">{
    private </span><span style="color: #0000BB">$name</span><span style="color: #007700">;

    public function </span><span style="color: #0000BB">__construct</span><span style="color: #007700">()
    {
        </span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();
    }

    public function </span><span style="color: #0000BB">tell</span><span style="color: #007700">()
    {
        if (isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">)) {
            echo </span><span style="color: #DD0000">"I am </span><span style="color: #007700">{</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">}</span><span style="color: #DD0000">.\n"</span><span style="color: #007700">;
        } else {
            echo </span><span style="color: #DD0000">"I don't have a name yet.\n"</span><span style="color: #007700">;
        }
    }
}

</span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT * FROM people"</span><span style="color: #007700">);
</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setFetchMode</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_CLASS</span><span style="color: #007700">, </span><span style="color: #DD0000">'Person'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$person </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();
</span><span style="color: #0000BB">$person</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();
</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setFetchMode</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_CLASS</span><span style="color: #007700">|</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_PROPS_LATE</span><span style="color: #007700">, </span><span style="color: #DD0000">'Person'</span><span style="color: #007700">);
</span><span style="color: #0000BB">$person </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();
</span><span style="color: #0000BB">$person</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();
</span><span style="color: #0000BB">?&gt;</span></code></pre></div>
   </div>

   <div class="example-contents"><p>Поданий вище приклад виведе щось
схоже на:</p></div>
   <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">I am Alice.
I am Alice.
I don&#039;t have a name yet.
I am Bob.</pre>
</div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetch-seealso">
  <h3 class="title">Прогляньте також</h3>
  <ul class="simplelist">
   <li><span class="methodname"><a href="pdo.prepare.php" class="methodname" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</span></li>
   <li><span class="methodname"><a href="pdostatement.execute.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</span></li>
   <li><span class="methodname"><a href="pdostatement.fetchall.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - Fetches the remaining rows from a result set</span></li>
   <li><span class="methodname"><a href="pdostatement.fetchcolumn.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</span></li>
   <li><span class="methodname"><a href="pdostatement.fetchobject.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetchObject()</a> - Fetches the next row and returns it as an object</span></li>
   <li><span class="methodname"><a href="pdostatement.setfetchmode.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - Set the default fetch mode for this statement</span></li>
  </ul>
 </div>

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