Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 11273

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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>Fetches the next row from a result set</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.execute.html">PDOStatement::execute</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchall.html">PDOStatement::fetchAll</a></div>
 <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><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, 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">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>PDOStatement::fetch</strong></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$fetch_style</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$cursor_orientation</code><span class="initializer"> = PDO::FETCH_ORI_NEXT</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$cursor_offset</code><span class="initializer"> = 0</span></span>
  ]]] )</div>


  <p class="para rdfs-comment">
   Fetches a row from a result set associated with a PDOStatement object. The
   <code class="parameter">fetch_style</code> parameter determines how PDO returns
   the row.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.fetch-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">fetch_style</code></dt>

     <dd>

      <p class="para">
       Controls how the next row will be returned to the caller. This value
       must be one of the <em>PDO::FETCH_*</em> constants,
       defaulting to value of <em>PDO::ATTR_DEFAULT_FETCH_MODE</em>
       (which defaults to <em>PDO::FETCH_BOTH</em>).
       <ul class="itemizedlist">
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_ASSOC</em>: returns an array indexed by column
         name as returned in your result set
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_BOTH</em> (default): returns an array indexed by
         both column name and 0-indexed column number as returned in your
         result set
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_BOUND</em>: returns <strong><code>TRUE</code></strong> and assigns the
         values of the columns in your result set to the PHP variables to which
         they were bound with the <span class="function"><a href="pdostatement.bindcolumn.html" class="function">PDOStatement::bindColumn()</a></span>
         method
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_CLASS</em>: returns a new instance of the
         requested class, mapping the columns of the result set to named
         properties in the class, and calling the constructor afterwards, unless
         <em>PDO::FETCH_PROPS_LATE</em> is also given.
         If <code class="parameter">fetch_style</code>
         includes PDO::FETCH_CLASSTYPE (e.g. <em>PDO::FETCH_CLASS |
         PDO::FETCH_CLASSTYPE</em>) then the name of the class is
         determined from a value of the first column.
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_INTO</em>: 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="para">
         <em>PDO::FETCH_LAZY</em>: combines
         <em>PDO::FETCH_BOTH</em> and <em>PDO::FETCH_OBJ</em>,
         creating the object variable names as they are accessed
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_NAMED</em>: returns an array with the same
         form as <em>PDO::FETCH_ASSOC</em>, 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="para">
         <em>PDO::FETCH_NUM</em>: returns an array indexed by column
         number as returned in your result set, starting at column 0
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_OBJ</em>: returns an anonymous object with
         property names that correspond to the column names returned in your
         result set
        </p></li>
        <li class="listitem"><p class="para">
         <em>PDO::FETCH_PROPS_LATE</em>: when used with
         <em>PDO::FETCH_CLASS</em>, the constructor of the class is
         called before the properties are assigned from the respective column
         values.
        </p></li>
       </ul>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">cursor_orientation</code></dt>

     <dd>

      <p class="para">
       For a PDOStatement object representing a scrollable cursor, this
       value determines which row will be returned to the caller. This value
       must be one of the <em>PDO::FETCH_ORI_*</em> constants,
       defaulting to <em>PDO::FETCH_ORI_NEXT</em>. To request a
       scrollable cursor for your PDOStatement object, you must set the
       <em>PDO::ATTR_CURSOR</em> attribute to
       <em>PDO::CURSOR_SCROLL</em> when you prepare the SQL
       statement with <span class="function"><a href="pdo.prepare.html" class="function">PDO::prepare()</a></span>.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para"> 
       For a PDOStatement object representing a scrollable cursor for which
       the <em>cursor_orientation</em> parameter is set to
       <em>PDO::FETCH_ORI_ABS</em>, this value specifies the
       absolute number of the row in the result set that shall be fetched.
      </p>
      <p class="para">
       For a PDOStatement object representing a scrollable cursor for which
       the <em>cursor_orientation</em> parameter is set to
       <em>PDO::FETCH_ORI_REL</em>, this value specifies the
       row to fetch relative to the cursor position before
       <span class="function"><strong>PDOStatement::fetch()</strong></span> was called.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetch-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   The return value of this function on success depends on the fetch type. In
   all cases, <strong><code>FALSE</code></strong> is returned on failure.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetch-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1071"><p><strong>Example #1 Fetching rows using different fetch styles</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$sth&nbsp;</span><span style="color: #007700">=&nbsp;</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: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;Exercise&nbsp;PDOStatement::fetch&nbsp;styles&nbsp;*/<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"PDO::FETCH_ASSOC:&nbsp;"</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"Return&nbsp;next&nbsp;row&nbsp;as&nbsp;an&nbsp;array&nbsp;indexed&nbsp;by&nbsp;column&nbsp;name\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /><br />print(</span><span style="color: #DD0000">"PDO::FETCH_BOTH:&nbsp;"</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"Return&nbsp;next&nbsp;row&nbsp;as&nbsp;an&nbsp;array&nbsp;indexed&nbsp;by&nbsp;both&nbsp;column&nbsp;name&nbsp;and&nbsp;number\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /><br />print(</span><span style="color: #DD0000">"PDO::FETCH_LAZY:&nbsp;"</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"Return&nbsp;next&nbsp;row&nbsp;as&nbsp;an&nbsp;anonymous&nbsp;object&nbsp;with&nbsp;column&nbsp;names&nbsp;as&nbsp;properties\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /><br />print(</span><span style="color: #DD0000">"PDO::FETCH_OBJ:&nbsp;"</span><span style="color: #007700">);<br />print(</span><span style="color: #DD0000">"Return&nbsp;next&nbsp;row&nbsp;as&nbsp;an&nbsp;anonymous&nbsp;object&nbsp;with&nbsp;column&nbsp;names&nbsp;as&nbsp;properties\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</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">);<br />print&nbsp;</span><span style="color: #0000BB">$result</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">;<br />print(</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
PDO::FETCH_ASSOC: Return next row as an array indexed by column name
Array
(
    [name] =&gt; apple
    [colour] =&gt; red
)

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

PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties
PDORow Object
(
    [name] =&gt; orange
    [colour] =&gt; orange
)

PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties
kiwi
</pre></div>
    </div>
   </div>
   <div class="example" id="example-1072"><p><strong>Example #2 Fetching rows with a scrollable cursor</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">readDataForwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;hand,&nbsp;won,&nbsp;bet&nbsp;FROM&nbsp;mynumbers&nbsp;ORDER&nbsp;BY&nbsp;BET'</span><span style="color: #007700">;<br />&nbsp;&nbsp;try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_SCROLL</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_NEXT</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />&nbsp;&nbsp;}<br />}<br />function&nbsp;</span><span style="color: #0000BB">readDataBackwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$sql&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SELECT&nbsp;hand,&nbsp;won,&nbsp;bet&nbsp;FROM&nbsp;mynumbers&nbsp;ORDER&nbsp;BY&nbsp;bet'</span><span style="color: #007700">;<br />&nbsp;&nbsp;try&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_CURSOR&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">CURSOR_SCROLL</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_LAST</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\t"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">2</span><span style="color: #007700">]&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$data</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;while&nbsp;(</span><span style="color: #0000BB">$row&nbsp;</span><span style="color: #007700">=&nbsp;</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">,&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_ORI_PRIOR</span><span style="color: #007700">));<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$stmt&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;catch&nbsp;(</span><span style="color: #0000BB">PDOException&nbsp;$e</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;print&nbsp;</span><span style="color: #0000BB">$e</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />&nbsp;&nbsp;}<br />}<br /><br />print&nbsp;</span><span style="color: #DD0000">"Reading&nbsp;forwards:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">readDataForwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /><br />print&nbsp;</span><span style="color: #DD0000">"Reading&nbsp;backwards:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">readDataBackwards</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
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-1073"><p><strong>Example #3 Construction order</strong></p>
    <div class="example-contents"><p>
     When objects are fetched via <em>PDO::FETCH_CLASS</em> the object
     properties are assigned first, and then the constructor of the class is
     invoked. If <em>PDO::FETCH_PROPS_LATE</em> 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"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">class&nbsp;</span><span style="color: #0000BB">Person<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">__construct</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;function&nbsp;</span><span style="color: #0000BB">tell</span><span style="color: #007700">()<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(isset(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">name</span><span style="color: #007700">))&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;am&nbsp;</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">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"I&nbsp;don't&nbsp;have&nbsp;a&nbsp;name&nbsp;yet.\n"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />}<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</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&nbsp;*&nbsp;FROM&nbsp;people"</span><span style="color: #007700">);<br /></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">,&nbsp;</span><span style="color: #DD0000">'Person'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$person&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$person</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();<br /></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">,&nbsp;</span><span style="color: #DD0000">'Person'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$person&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetch</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$person</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">tell</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
I am Alice.
I am Alice.
I don&#039;t have a name yet.
I am Bob.
</pre></div>
    </div>
   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetch-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</span></li>
    <li class="member"><span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetchall.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchAll()</a> - Returns an array containing all of the result set rows</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</span></li>
    <li class="member"><span class="function"><a href="pdostatement.fetchobject.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchObject()</a> - Fetches the next row and returns it as an object</span></li>
    <li class="member"><span class="function"><a href="pdostatement.setfetchmode.html" class="function" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - Set the default fetch mode for this statement</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="pdostatement.execute.html">PDOStatement::execute</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchall.html">PDOStatement::fetchAll</a></div>
 <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>