Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 10220

php-manual-en-5.5.7-1.mga4.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>Returns an array containing all of the result set rows</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.fetch.html">PDOStatement::fetch</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchcolumn.html">PDOStatement::fetchColumn</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.fetchall" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::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::fetchAll</span> &mdash; <span class="dc-title">
   Returns an array containing all of the result set rows
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.fetchall-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="type">array</span> <span class="methodname"><strong>PDOStatement::fetchAll</strong></span>
    ([ <span class="methodparam"><span class="type">int</span> <code class="parameter">$fetch_style</code></span>
   [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$fetch_argument</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$ctor_args</code><span class="initializer"> = array()</span></span>
  ]]] )</div>


 </div>


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

    <dt>

     <span class="term"><em><code class="parameter">fetch_style</code></em></span>
     <dd>

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

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">fetch_argument</code></em></span>
     <dd>

      <p class="para">
       This argument have a different meaning depending on the value of 
       the <em><code class="parameter">fetch_style</code></em> parameter:
       <ul class="itemizedlist">
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_COLUMN</code></strong>: Returns the indicated 0-indexed 
           column.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_CLASS</code></strong>: Returns instances of the specified
          class, mapping the columns of each row to named properties in the class.
         </p>
        </li>
        <li class="listitem">
         <p class="para">
          <strong><code>PDO::FETCH_FUNC</code></strong>: Returns the results of calling the
          specified function, using each row&#039;s columns as parameters in the call.
         </p>
        </li>
       </ul>
      </p>
     </dd>

    </dt>

    <dt>

     <span class="term"><em><code class="parameter">ctor_args</code></em></span>
     <dd>

      <p class="para">
       Arguments of custom class constructor when the <em><code class="parameter">fetch_style</code></em> 
       parameter is <strong><code>PDO::FETCH_CLASS</code></strong>.
      </p>
     </dd>

    </dt>

   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-pdostatement.fetchall-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
    <span class="function"><strong>PDOStatement::fetchAll()</strong></span> 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. An empty array is returned if there
   are zero results to fetch, or <strong><code>FALSE</code></strong> on failure.
  </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
   ORDER BY clauses in SQL to restrict results before retrieving and
   processing them with PHP.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdostatement.fetchall-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-952"><p><strong>Example #1 Fetch all remaining rows in a result set</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;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"><p>The above example will output
something similar to:</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>
   <div class="example" id="example-953"><p><strong>Example #2 Fetching all values of a single column from a result set</strong></p>
    <div class="example-contents"><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">
<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"><p>The above example will output
something similar to:</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>
   <div class="example" id="example-954"><p><strong>Example #3 Grouping all values by a single column</strong></p>
    <div class="example-contents"><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 <em>apple</em> and
     <em>pear</em> are returned as arrays that contain two
     different colours, while <em>watermelon</em> is
     returned as an array that contains only one colour.
    </p></div>
    <div class="example-contents">
<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"><p>The above example will output
something similar to:</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>
   <div class="example" id="example-955"><p><strong>Example #4 Instantiating a class for each result</strong></p>
    <div class="example-contents"><p>
     The following example demonstrates the behaviour of the
     <strong><code>PDO::FETCH_CLASS</code></strong> fetch style.
    </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">fruit&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;</span><span style="color: #0000BB">$colour</span><span style="color: #007700">;<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">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: #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_CLASS</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"fruit"</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"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  object(fruit)#1 (2) {
    [&quot;name&quot;]=&gt;
    string(5) &quot;apple&quot;
    [&quot;colour&quot;]=&gt;
    string(5) &quot;green&quot;
  }
  [1]=&gt;
  object(fruit)#2 (2) {
    [&quot;name&quot;]=&gt;
    string(4) &quot;pear&quot;
    [&quot;colour&quot;]=&gt;
    string(6) &quot;yellow&quot;
  }
  [2]=&gt;
  object(fruit)#3 (2) {
    [&quot;name&quot;]=&gt;
    string(10) &quot;watermelon&quot;
    [&quot;colour&quot;]=&gt;
    string(4) &quot;pink&quot;
  }
}
</pre></div>
    </div>
   </div>
   <div class="example" id="example-956"><p><strong>Example #5 Calling a function for each result</strong></p>
    <div class="example-contents"><p>
     The following example demonstrates the behaviour of the
     <strong><code>PDO::FETCH_FUNC</code></strong> fetch style.
    </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">function&nbsp;</span><span style="color: #0000BB">fruit</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$colour</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"</span><span style="color: #007700">{</span><span style="color: #0000BB">$name</span><span style="color: #007700">}</span><span style="color: #DD0000">:&nbsp;</span><span style="color: #007700">{</span><span style="color: #0000BB">$colour</span><span style="color: #007700">}</span><span style="color: #DD0000">"</span><span style="color: #007700">;<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">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: #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_FUNC</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"fruit"</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"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(3) {
  [0]=&gt;
  string(12) &quot;apple: green&quot;
  [1]=&gt;
  string(12) &quot;pear: yellow&quot;
  [2]=&gt;
  string(16) &quot;watermelon: pink&quot;
}
</pre></div>
    </div>
   </div>

  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-pdostatement.fetchall-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"> <span class="function"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement object</span></li>
    <li class="member"> <span class="function"><a href="pdostatement.fetch.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetch()</a> - Fetches the next row from a result set</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="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.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.fetch.html">PDOStatement::fetch</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="pdostatement.fetchcolumn.html">PDOStatement::fetchColumn</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>