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->fetchAll</h1>
<p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement->fetchAll</span> — <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"><?php<br />$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></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">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Fetch all of the remaining rows in the result set */<br /></span><span style="color: #007700">print(</span><span style="color: #DD0000">"Fetch all of the remaining rows in the result set:\n"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></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">?></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] => Array
(
[NAME] => pear
[0] => pear
[COLOUR] => green
[1] => green
)
[1] => Array
(
[NAME] => watermelon
[0] => watermelon
[COLOUR] => pink
[1] => 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"><?php<br />$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></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">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Fetch all of the values of the first column */<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></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">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">?></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] =>
string(5) => apple
[1] =>
string(4) => pear
[2] =>
string(10) => 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"><?php<br />$insert </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"INSERT INTO fruit(name, colour) VALUES (?, ?)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'apple'</span><span style="color: #007700">, </span><span style="color: #DD0000">'green'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">$insert</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'pear'</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow'</span><span style="color: #007700">));<br /><br /></span><span style="color: #0000BB">$sth </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></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">);<br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Group values by the first column */<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-></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">?></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) {
["apple"]=>
array(2) {
[0]=>
string(5) "green"
[1]=>
string(3) "red"
}
["pear"]=>
array(2) {
[0]=>
string(5) "green"
[1]=>
string(6) "yellow"
}
["watermelon"]=>
array(1) {
[0]=>
string(5) "green"
}
}
</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(); ?>