Latest web development tutorials

PDOStatement :: fetchAll

PHP PDO Reference Manual PHP PDO Reference Manual

PDOStatement :: fetchAll - returns an array of all the rows in the result set (PHP 5> = 5.1.0, PECL pdo> = 0.1.0)


Explanation

grammar

array PDOStatement::fetchAll ([ int $fetch_style [, mixed $fetch_argument [, array $ctor_args = array() ]]] )

parameter

fetch_style

How to control the next line is returned to the caller. This value must be PDO :: FETCH_ * constants in a series, the default value for the PDO :: ATTR_DEFAULT_FETCH_MODE (default PDO :: FETCH_BOTH).

To return a result set that contains an array of all the values ​​in a single column, you need to specify PDO :: FETCH_COLUMN. Get the desired columns by specifying column-index parameter.

Want to get the result set of unique values ​​in a single column, you need to PDO :: FETCH_COLUMN and PDO :: FETCH_UNIQUE bitwise or.

To return a column based on the specified value of the associative array grouped, we need to PDO :: FETCH_COLUMN and PDO :: FETCH_GROUP bitwise or.

fetch_argument
Fetch_style parameter based on the value of this parameter has a different meaning:

  • PDO::FETCH_COLUMN : Returns to the zero-indexed columns.

  • PDO::FETCH_CLASS : returns an instance of the specified class, mapping column for each row corresponding to the class attribute name.

  • PDO::FETCH_FUNC : the column of each row as an argument to the specified function, and returns the result after calling function.

ctor_args
When fetch_style parameters PDO :: FETCH_CLASS, since the definition of the class constructor parameters.


return value

PDOStatement :: fetchAll () returns a result set that contains an array of all the remaining lines. Each row of this array is an array or a column value, or an object attribute corresponding to each column name.

Use this method to obtain a large result set will cause the system burden and may take up a lot of network resources. Retrieve all of the data and its use PHP to operate, it would be better to consider the use of database services to deal with the result set. For example, prior to and retrieve data through the PHP process to define the results with WHERE and ORDER BY clauses in SQL.


Examples

Get all of the remaining rows in the result set

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* 获取结果集中所有剩余的行 */
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
        )

)

Being a single result set all values

The following example demonstrates how to concentrate a separate return all values ​​from a result, even though the SQL statement itself may return multiple columns per row.

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* 获取第一列所有值 */
$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
)

According to a separate grouping all values

The following example demonstrates how to return an associative array centralized specified column value based on the results of the packet. The array contains three keys: apple and pear returned array contains two different colors, while watermelon returned array contains only one color.

<?php
$insert = $dbh->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();

/* 根据第一列分组  */
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"
  }
}

Each row in the result instantiate a class

Liezi below demonstrates PDO :: FETCH_CLASS Gets style behavior.

<?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);
?>

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"
  }
}

Each line calls a function

Liezi below demonstrates PDO :: FETCH_FUNC Gets style behavior.

<?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);
?>

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 PHP PDO Reference Manual