(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
PDOStatement::fetchAll — ����һ������������������е�����
$fetch_style
[, mixed $fetch_argument
[, array $ctor_args = array()
]]] ) : array
fetch_style
���Ʒ��������������ͬ PDOStatement::fetch() �ĵ��м��ص�һ����Ĭ��Ϊ PDO::ATTR_DEFAULT_FETCH_MODE ��ֵ�� ��ȱʡֵΪ PDO::FETCH_BOTH ��
��Ҫ����һ������������е���һ������ֵ�����飬��Ҫָ�� PDO::FETCH_COLUMN ��ͨ��ָ�� column-index ������ȡ��Ҫ���С�
��Ҫ��ȡ������е���һ�е�Ψһֵ����Ҫ�� PDO::FETCH_COLUMN �� PDO::FETCH_UNIQUE ��λ��
��Ҫ����һ������ָ���а�ֵ�����Ĺ������飬��Ҫ�� PDO::FETCH_COLUMN �� PDO::FETCH_GROUP ��λ��
fetch_argument
���� fetch_style ������ֵ���˲����в�ͬ�����壺
PDO::FETCH_COLUMN������ָ����0��ʼ�������С�
PDO::FETCH_CLASS������ָ�����ʵ����ӳ��ÿ�е��е����ж�Ӧ����������
PDO::FETCH_FUNC����ÿ�е�����Ϊ�������ݸ�ָ���ĺ����������ص��ú�����Ľ����
ctor_args
�� fetch_style ����Ϊ PDO::FETCH_CLASS ʱ���Զ�����Ĺ��캯���IJ�����
PDOStatement::fetchAll() ����һ�����������������ʣ���е����顣�������ÿһ��Ҫô��һ����ֵ�����飬Ҫô�����Զ�Ӧÿ��������һ������
ʹ�ô˷�����ȡ������������ϵͳ���������ҿ���ռ�ô���������Դ������ȡ���������ݺ���PHP�������������翼��ʹ�����ݿ�������������������磬��ȡ�����ݲ�ͨ��PHP����ǰ���� SQL ��ʹ�� WHERE �� ORDER BY �Ӿ����������
Example #1 ��ȡ�����������ʣ�����
<?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);
?>
�������̵���������ڣ�
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 ��ȡ������е���һ�е�����ֵ
����������ʾ����δ�һ��������з��ص���һ�����е�ֵ������ SQL ���������ܷ���ÿ�ж��С�
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* ��ȡ��һ������ֵ */
$result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
var_dump($result);
?>
�������̵���������ڣ�
Array(3)
(
[0] =>
string(5) => apple
[1] =>
string(4) => pear
[2] =>
string(10) => watermelon
)
Example #3 ���ݵ�����һ�а�����ֵ����
����������ʾ����η���һ�����ݽ������ָ���е�ֵ����Ĺ������顣��������������������ص� apple �� pear ������������ֲ�ͬ����ɫ�������ص� watermelon ���������һ����ɫ��
<?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));
?>
�������̵���������ڣ�
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 ÿ�н��ʵ����һ����
����������ʾ�� PDO::FETCH_CLASS ��ȡ������Ϊ��
<?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);
?>
�������̵���������ڣ�
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 ÿ�е���һ�κ���
����������ʾ�� PDO::FETCH_FUNC ��ȡ������Ϊ��
<?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);
?>
�������̵���������ڣ�
array(3) {
[0]=>
string(12) "apple: green"
[1]=>
string(12) "pear: yellow"
[2]=>
string(16) "watermelon: pink"
}