Hello everyone,I've done some polishing to the PDOStatement::fetchAll documentation. Attached is the patch, as well as the resulting chunked-render html.
Summary:Changed the fetch_argument parameter type to mixed, as it differs per fetch_style, and removed the initializer.
Changed a few instances of <literal> to <constant> where appropriate.Clarified wording of PDO::FETCH_CLASS behaviour for the fetch_argument parameter.
Added the PDO::FETCH_FUNC behavior for the fetch_argument parameter. Added two examples, for PDO::FETCH_CLASS and PDO::FETCH_FUNC styles. Thanks, Justin Martin, a.k.a. FrozenFire
Index: en/reference/pdo/pdostatement/fetchall.xml =================================================================== --- en/reference/pdo/pdostatement/fetchall.xml (revision 305106) +++ en/reference/pdo/pdostatement/fetchall.xml (working copy) @@ -12,7 +12,7 @@ <methodsynopsis> <type>array</type><methodname>PDOStatement::fetchAll</methodname> <methodparam choice="opt"><type>int</type><parameter>fetch_style</parameter><initializer>PDO::FETCH_BOTH</initializer></methodparam> - <methodparam choice="opt"><type>int</type><parameter>fetch_argument</parameter><initializer>0</initializer></methodparam> + <methodparam choice="opt"><type>mixed</type><parameter>fetch_argument</parameter></methodparam> <methodparam choice="opt"><type>array</type><parameter>ctor_args</parameter><initializer>array()</initializer></methodparam> </methodsynopsis> @@ -31,19 +31,19 @@ </para> <para> To return an array consisting of all values of a single column from - the result set, specify <literal>PDO::FETCH_COLUMN</literal>. You + the result set, specify <constant>PDO::FETCH_COLUMN</constant>. You can specify which column you want with the <parameter>column-index</parameter> parameter. </para> <para> To fetch only the unique values of a single column from the result set, - bitwise-OR <literal>PDO::FETCH_COLUMN</literal> with - <literal>PDO::FETCH_UNIQUE</literal>. + bitwise-OR <constant>PDO::FETCH_COLUMN</constant> with + <constant>PDO::FETCH_UNIQUE</constant>. </para> <para> To return an associative array grouped by the values of a specified - column, bitwise-OR <literal>PDO::FETCH_COLUMN</literal> with - <literal>PDO::FETCH_GROUP</literal>. + column, bitwise-OR <constant>PDO::FETCH_COLUMN</constant> with + <constant>PDO::FETCH_GROUP</constant>. </para> </listitem> </varlistentry> @@ -62,10 +62,16 @@ </listitem> <listitem> <para> - <constant>PDO::FETCH_CLASS</constant>: Sets a custom class name for which - the fetch results will be stored in. + <constant>PDO::FETCH_CLASS</constant>: Returns instances of the specified + class, mapping the columns of each row to named properties in the class. </para> </listitem> + <listitem> + <para> + <constant>PDO::FETCH_FUNC</constant>: Returns the results of calling the + specified function, using each row's columns as parameters in the call. + </para> + </listitem> </itemizedlist> </para> </listitem> @@ -229,7 +235,91 @@ ]]> </screen> </example> + <example><title>Instantiating a class for each result</title> + <para> + The following example demonstrates the behaviour of the + <constant>PDO::FETCH_CLASS</constant> fetch style. + </para> + <programlisting role="php"> +<![CDATA[ +<?php +class fruit { + public $name; + public $colour; +} +$sth = $dbh->prepare("SELECT name, colour FROM fruit"); +$sth->execute(); + +$result = $sth->fetchAll(PDO::FETCH_CLASS, "fruit"); +var_dump($result); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +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" + } +} +]]> + </screen> + </example> + <example><title>Calling a function for each result</title> + <para> + The following example demonstrates the behaviour of the + <constant>PDO::FETCH_FUNC</constant> fetch style. + </para> + <programlisting role="php"> +<![CDATA[ +<?php +function fruit($name, $colour) { + return "{$name}: {$colour}"; +} + +$sth = $dbh->prepare("SELECT name, colour FROM fruit"); +$sth->execute(); + +$result = $sth->fetchAll(PDO::FETCH_FUNC, "fruit"); +var_dump($result); +?> +]]> + </programlisting> + &example.outputs; + <screen> +<![CDATA[ +array(3) { + [0]=> + string(12) "apple: green" + [1]=> + string(12) "pear: yellow" + [2]=> + string(16) "watermelon: pink" +} +]]> + </screen> + </example> + </para> </refsect1>Title: PHP Manual: Returns an array containing all of the result set rows
|
PDOStatement->fetchAllReturns an array containing all of the result set rows Description
array PDOStatement::fetchAll
([ int $fetch_style = PDO::FETCH_BOTH
[, mixed $fetch_argument
[, array $ctor_args = array()
]]] )
Parameters
Return ValuesPDOStatement::fetchAll 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. 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. Examples
Example #1 Fetch all remaining rows in a result set
<?php The above example will output: 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 ) ) Example #2 Fetching all values of a single column from a result set 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.
<?php The above example will output: Array(3) ( [0] => string(5) => apple [1] => string(4) => pear [2] => string(10) => watermelon ) Example #3 Grouping all values by a single column 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 apple and pear are returned as arrays that contain two different colours, while watermelon is returned as an array that contains only one colour.
<?php The above example will output: 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" } } Example #4 Instantiating a class for each result The following example demonstrates the behaviour of the PDO::FETCH_CLASS fetch style.
<?php The above example will output: 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" } } Example #5 Calling a function for each result The following example demonstrates the behaviour of the PDO::FETCH_FUNC fetch style.
<?php The above example will output: array(3) { [0]=> string(12) "apple: green" [1]=> string(12) "pear: yellow" [2]=> string(16) "watermelon: pink" } See Also
|