YouTip LogoYouTip

Pdostatement Fetchall

[![Image 1: PHP PDO Reference Manual](#)PHP PDO Reference Manual](#) PDOStatement::fetchAll β€” Returns an array containing all of the result set rows (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 will be 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 the values of a single column from the result set, specify PDO::FETCH_COLUMN. Use the column-index parameter to specify the column you want. To retrieve unique values for a single column from the result set, bitwise OR PDO::FETCH_COLUMN with PDO::FETCH_UNIQUE. To return an associative array grouped by the 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 argument has a different meaning: * **`PDO::FETCH_COLUMN`**: Returns the column specified by the 0-based index. * **`PDO::FETCH_CLASS`**: Returns a new instance of the specified class, mapping each row's columns to the corresponding property names in the class. * **`PDO::FETCH_FUNC`**: Passes each row's columns as arguments to the specified function and returns the result of the function call. **ctor_args** When the fetch_style parameter is PDO::FETCH_CLASS, this array contains the arguments for the custom class's constructor. * * * ## Return Values PDOStatement::fetchAll() returns an array containing all of the remaining rows in the result set. Each row in the array is either an array of column values or an object with properties corresponding to each column name. Using this method to fetch large result sets can cause the system to become burdened and may consume a lot of network resources. Instead of fetching all data and then manipulating it with PHP, consider using the database service to process the result set. For example, use WHERE and ORDER BY clauses in the SQL to limit the results before fetching and processing them with PHP. * * * ## Examples ### Fetching all remaining rows in the result set prepare("SELECT name, colour FROM fruit"); $sth->execute();/* Fetch 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( => Array ( => pear => pear => green => green ) => Array ( => watermelon => watermelon => pink => pink )) ### Fetching all values of a single column from the result set The following example demonstrates how to return all 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();/* Fetch all values of the first column */ $result = $sth->fetchAll(PDO::FETCH_COLUMN, 0); var_dump($result);?> The output of the above example is: Array(3)( => string(5) => apple => string(4) => pear => string(10) => watermelon ) ### Grouping all values by a single column The following example 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 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) { => array(2) { => string(5) "green" => string(3) "red" } => array(2) { => string(5) "green" => string(6) "yellow" } => array(1) { => string(5) "green" }} ### Instantiating a class for each row The following example 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) { => object(fruit)#1 (2) { => string(5) "apple" => string(5) "green" } => object(fruit)#2 (2) { => string(4) "pear" => string(6) "yellow" } => object(fruit)#3 (2) { => string(10) "watermelon" => string(4) "pink" }} ### Calling a function for each row The following example 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) { => string(12) "apple: green" => string(12) "pear: yellow" => string(16) "watermelon: pink"} * * PHP PDO Reference Manual](#)
← Pdostatement FetchcolumnPdostatement Fetch β†’