PDOStatement::fetchAll
PDOStatement::fetchAll — Returns an array containing all of the rows in the result set (PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
Description
Syntax
array PDOStatement::fetchAll ([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array() ]]] )
Parameters
** fetch_style**
Controls how the next row is returned to the caller. This value must be one of the PDO::FETCH_* series of constants, defaulting to the value of PDO::ATTR_DEFAULT_FETCH_MODE (which defaults to PDO::FETCH_BOTH).
To return an array containing all values of a single column from the result set, specify PDO::FETCH_COLUMN. Specify the column-index parameter to get the desired column.
To fetch only the unique values of a single column from the result set, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_UNIQUE.
To return an associative array grouped by values of a specified column, bitwise-OR PDO::FETCH_COLUMN with PDO::FETCH_GROUP.
fetch_argument
Depending on the value of the fetch_style parameter, this parameter has different meanings:
-
PDO::FETCH_COLUMN: Returns the specified column indexed starting at 0. -
PDO::FETCH_CLASS: Returns an instance of the specified class, mapping the columns of each row to properties with the same name in the class. -
PDO::FETCH_FUNC: Passes the columns of each row as arguments to the specified function, and returns the result of calling the function.
ctor_args
The arguments to the constructor of the custom class when the fetch_style parameter is PDO::FETCH_CLASS.
Return Value
PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. Each row in this array is either an array of column values or an object with properties corresponding to each column name.
Fetching a large result set with this method will increase system load and may consume significant network resources. Rather than retrieving all data and processing it with PHP, consider using the database service to process the result set. For example, use WHERE and ORDER BY clauses in SQL to limit the results before retrieving the data and processing it with PHP.
Example
Fetch all remaining rows in the result set
prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Get all remaining rows in the result set */
print("Fetch all of the remaining rows in the result set:\n");
$result = $sth->fetchAll();
print_r($result);
?>
The output of the above example is:
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
)
)
Fetch all values of a single column from the result set
The example below demonstrates how to return all the values of a single column from a result set, even though the SQL statement itself may return multiple columns per row.
prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Get all values in the first column */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>
The output of the above example is:
Array(3)
(
[0] =>
string(5) => apple
[1] =>
string(4) => pear
[2] =>
string(10) => watermelon
)
Group all values by a single column
The example below demonstrates how to return an associative array grouped by the values of a specified column in the result set. The array contains three keys: the returned apple and pear arrays each contain two different colors, while the returned watermelon array contains only one color.
prepare("INSERT INTO fruit(name, colour) VALUES (?, ?)");
$insert->execute(array('apple', 'green'));
$insert->execute(array('pear', 'yellow'));
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Group by the first column */
var_dump($sth->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_GROUP));
?>
The output of the above example is:
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"
}
}
Instantiating a class per result row
The example below demonstrates the behavior of the PDO::FETCH_CLASS fetch style.
prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_CLASS, "fruit");
var_dump($result);
?>
The output of the above example is:
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"
}
}
Calling a function per row
The example below demonstrates the behavior of the PDO::FETCH_FUNC fetch style.
prepare("SELECT name, colour FROM fruit");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_FUNC, "fruit");
var_dump($result);
?>
The output of the above example is:
array(3) {
[0]=>
string(12) "apple: green"
[1]=>
string(12) "pear: yellow"
[2]=>
string(16) "watermelon: pink"
}
PHP PDO Reference Manual