downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/pdostatement.fetchall.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'pdostatement.fetchall.php',
   
1 => 'PDOStatement->fetchAll',
  ),
 
'up' =>
  array (
   
0 => 'class.pdostatement.php',
   
1 => 'The PDOStatement class',
  ),
 
'prev' =>
  array (
   
0 => 'pdostatement.fetch.php',
   
1 => 'PDOStatement->fetch',
  ),
 
'next' =>
  array (
   
0 => 'pdostatement.fetchcolumn.php',
   
1 => 'PDOStatement->fetchColumn',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="pdostatement.fetchall" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement-&gt;fetchAll</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement-&gt;fetchAll</span> &mdash; <span class="dc-title">
   Returns an array containing all of the result set rows
  </span></p>

 </div>
 <a name="pdostatement.fetchall.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><b>PDOStatement::fetchAll</b></span>
    ([ <span class="methodparam"><span class="type">int</span> <tt class="parameter">$fetch_style</tt><span class="initializer"> = PDO::FETCH_BOTH</span></span>
   [, <span class="methodparam"><span class="type">int</span> <tt class="parameter">$column_index</tt></span>
   [, <span class="methodparam"><span class="type">array</span> <tt class="parameter">$ctor_args</tt><span class="initializer"> = array()</span></span>
  ]]] )</div>


 </div>


 <a name="pdostatement.fetchall.parameters"></a><div class="refsect1 parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   </p><dl>

    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">fetch_style</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Controls the contents of the returned array as documented in
       <a href="pdostatement.fetch.php" class="function">PDOStatement::fetch()</a>. Defaults to
       <i>PDO::FETCH_BOTH</i>.
      </p>
      <p class="para">
       To return an array consisting of all values of a single column from
       the result set, specify <i>PDO::FETCH_COLUMN</i>. You
       can specify which column you want with the
       <i><tt class="parameter">column-index</tt></i>
 parameter.
      </p>
      <p class="para">
       To fetch only the unique values of a single column from the result set,
       bitwise-OR <i>PDO::FETCH_COLUMN</i> with
       <i>PDO::FETCH_UNIQUE</i>.
      </p>
      <p class="para">
       To return an associative array grouped by the values of a specified
       column, bitwise-OR <i>PDO::FETCH_COLUMN</i> with
       <i>PDO::FETCH_GROUP</i>.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">column_index</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Returns the indicated 0-indexed column when the value of
       <i><tt class="parameter">fetch_style</tt></i>
 is
       <i>PDO::FETCH_COLUMN</i>. Defaults to <i>0</i>.
      </p>
     </dd>

   
    <dt class="varlistentry">

     <span class="term"><i><tt class="parameter">ctor_args</tt></i>
</span>

     </dt><dd class="listitem">

      <p class="para">
       Arguments of custom class constructor.
      </p>
     </dd>

   
   </dl>
<p>
  </p>
 </div>


 <a name="pdostatement.fetchall.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <b>PDOStatement::fetchAll()</b> returns an array containing
   all of the remaining rows in the result set. The array represents each
   row as either an array of column values or an object with properties
   corresponding to each column name.
  </p>
  <p class="para">
   Using this method to fetch large result sets will result in a heavy
   demand on system and possibly network resources. Rather than retrieving
   all of the data and manipulating it in PHP, consider using the database
   server to manipulate the result sets. For example, use the WHERE and
   SORT BY clauses in SQL to restrict results before retrieving and
   processing them with PHP.
  </p>
 </div>


 <a name="pdostatement.fetchall.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   </p><div class="example"><p><b>Example #1 Fetch all remaining rows in a result set</b></p>
    <div class="example-contents programlisting">
<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;Fetch&nbsp;all&nbsp;of&nbsp;the&nbsp;remaining&nbsp;rows&nbsp;in&nbsp;the&nbsp;result&nbsp;set&nbsp;*/<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"Fetch&nbsp;all&nbsp;of&nbsp;the&nbsp;remaining&nbsp;rows&nbsp;in&nbsp;the&nbsp;result&nbsp;set:\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">fetchAll</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 /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Fetch all of the remaining rows in the result set:
Array
(
    [0] =&gt; Array
        (
            [NAME] =&gt; pear
            [0] =&gt; pear
            [COLOUR] =&gt; green
            [1] =&gt; green
        )

    [1] =&gt; Array
        (
            [NAME] =&gt; watermelon
            [0] =&gt; watermelon
            [COLOUR] =&gt; pink
            [1] =&gt; pink
        )

)
</pre></div>
    </div>
   </div><p>
   </p><div class="example"><p><b>Example #2 Fetching all values of a single column from a result set</b></p>
    <div class="example-contents para"><p>
     The following example demonstrates how to return all of the values of a
     single column from a result set, even though the SQL statement itself
     may return multiple columns per row.
    </p></div>
    <div class="example-contents programlisting">
<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;Fetch&nbsp;all&nbsp;of&nbsp;the&nbsp;values&nbsp;of&nbsp;the&nbsp;first&nbsp;column&nbsp;*/<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">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_COLUMN</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array(3)
(
    [0] =&gt;
    string(5) =&gt; apple
    [1] =&gt;
    string(4) =&gt; pear
    [2] =&gt;
    string(10) =&gt; watermelon
)
</pre></div>
    </div>
   </div><p>
   </p><div class="example"><p><b>Example #3 Grouping all values by a single column</b></p>
    <div class="example-contents para"><p>
     The following example demonstrates how to return an associative array
     grouped by the values of the specified column in the result set. The
     array contains three keys: values <i>apple</i> and
     <i>pear</i> are returned as arrays that contain two
     different colours, while <i>watermelon</i> is
     returned as an array that contains only one colour.
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$insert&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">"INSERT&nbsp;INTO&nbsp;fruit(name,&nbsp;colour)&nbsp;VALUES&nbsp;(?,&nbsp;?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'green'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'pear'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">));<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">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;Group&nbsp;values&nbsp;by&nbsp;the&nbsp;first&nbsp;column&nbsp;*/<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchAll</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_COLUMN</span><span style="color: #007700">|</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">FETCH_GROUP</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [&quot;apple&quot;]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;green&quot;
    [1]=&gt;
    string(3) &quot;red&quot;
  }
  [&quot;pear&quot;]=&gt;
  array(2) {
    [0]=&gt;
    string(5) &quot;green&quot;
    [1]=&gt;
    string(6) &quot;yellow&quot;
  }
  [&quot;watermelon&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(5) &quot;green&quot;
  }
}

</pre></div>
    </div>
   </div><p>

  </p>
 </div>


 <a name="pdostatement.fetchall.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="pdo.query.php" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement object</li>
    <li class="member"><a href="pdostatement.fetch.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - Fetches the next row from a result set</li>
    <li class="member"><a href="pdostatement.fetchcolumn.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</li>
    <li class="member"><a href="pdo.prepare.php" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</li>
    <li class="member"><a href="pdostatement.setfetchmode.php" class="function" rel="rdfs-seeAlso">PDOStatement::setFetchMode()</a> - Set the default fetch mode for this statement</li>
   </ul><p>
  </p>
 </div>

</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites