<!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 >= 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> <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'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"><?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"><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] => Array ( [NAME] => pear [0] => pear [COLOUR] => green [1] => green ) [1] => Array ( [NAME] => watermelon [0] => watermelon [COLOUR] => pink [1] => 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"><?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"><p>The above example will output something similar to:</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> <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"><?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"><p>The above example will output something similar to:</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> <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"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">fruit </span><span style="color: #007700">{<br /> public </span><span style="color: #0000BB">$name</span><span style="color: #007700">;<br /> public </span><span style="color: #0000BB">$colour</span><span style="color: #007700">;<br />}<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: #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_CLASS</span><span style="color: #007700">, </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">?></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]=> object(fruit)#1 (2) { ["name"]=> string(5) "apple" ["colour"]=> string(5) "green" } [1]=> object(fruit)#2 (2) { ["name"]=> string(4) "pear" ["colour"]=> string(6) "yellow" } [2]=> object(fruit)#3 (2) { ["name"]=> string(10) "watermelon" ["colour"]=> string(4) "pink" } } </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"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">fruit</span><span style="color: #007700">(</span><span style="color: #0000BB">$name</span><span style="color: #007700">, </span><span style="color: #0000BB">$colour</span><span style="color: #007700">) {<br /> return </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">: </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 </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: #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_FUNC</span><span style="color: #007700">, </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">?></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]=> string(12) "apple: green" [1]=> string(12) "pear: yellow" [2]=> string(16) "watermelon: pink" } </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>