(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
PDOStatement::fetch — �ӽ�����л�ȡ��һ��
$fetch_style
[, int $cursor_orientation
= PDO::FETCH_ORI_NEXT
[, int $cursor_offset
= 0
]]] ) : mixed
��һ�� PDOStatement ������صĽ�����л�ȡ��һ�С�fetch_style
�������� POD ��η����С�
fetch_style
������һ����η��ظ������ߡ���ֵ������ PDO::FETCH_* ϵ�г����е�һ����ȱʡΪ PDO::ATTR_DEFAULT_FETCH_MODE ��ֵ ��Ĭ��Ϊ PDO::FETCH_BOTH ����
PDO::FETCH_ASSOC������һ������Ϊ���������������
PDO::FETCH_BOTH��Ĭ�ϣ�������һ������Ϊ�������������0��ʼ���кŵ�����
PDO::FETCH_BOUND������ TRUE
�������������е���ֵ�� PDOStatement::bindColumn() ������ PHP ������
PDO::FETCH_CLASS������һ�����������ʵ����ӳ�������е����������ж�Ӧ������������� fetch_style
���� PDO::FETCH_CLASSTYPE�����磺PDO::FETCH_CLASS |
PDO::FETCH_CLASSTYPE�����������ɵ�һ�е�ֵ����
PDO::FETCH_INTO������һ�����������Ѵ��ڵ�ʵ����ӳ�������е��е���������������
PDO::FETCH_LAZY�����ʹ�� PDO::FETCH_BOTH �� PDO::FETCH_OBJ���������������ʵĶ��������
PDO::FETCH_NUM������һ������Ϊ��0��ʼ�Ľ�����кŵ�����
PDO::FETCH_OBJ������һ����������Ӧ�������������������
cursor_orientation
���� һ�� PDOStatement �����ʾ�Ŀɹ����α꣬��ֵ��������һ�н������ظ������ߡ���ֵ������ PDO::FETCH_ORI_* ϵ�г����е�һ����Ĭ��Ϊ PDO::FETCH_ORI_NEXT��Ҫ���� PDOStatement ����ʹ�ÿɹ����α꣬�������� PDO::prepare() Ԥ����SQL���ʱ������ PDO::ATTR_CURSOR ����Ϊ PDO::CURSOR_SCROLL��
offset
����һ�� cursor_orientation ��������Ϊ PDO::FETCH_ORI_ABS ��PDOStatement �������Ŀɹ����α꣬��ֵָ�����������Ҫ��ȡ�еľ����кš�
����һ�� cursor_orientation ��������Ϊ PDO::FETCH_ORI_REL ��PDOStatement �������Ŀɹ����α꣬��ֵָ����Ҫ��ȡ������ڵ��� PDOStatement::fetch() ǰ�α��λ��
�˺������������ɹ�ʱ���ص�ֵ��������ȡ���͡�����������£�ʧ�ܶ����� FALSE
��
Example #1 ʹ�ò�ͬ����ȡ��ʽ��ȡ��
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* ���� PDOStatement::fetch ��� */
print("PDO::FETCH_ASSOC: ");
print("Return next row as an array indexed by column name\n");
$result = $sth->fetch(PDO::FETCH_ASSOC);
print_r($result);
print("\n");
print("PDO::FETCH_BOTH: ");
print("Return next row as an array indexed by both column name and number\n");
$result = $sth->fetch(PDO::FETCH_BOTH);
print_r($result);
print("\n");
print("PDO::FETCH_LAZY: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO::FETCH_LAZY);
print_r($result);
print("\n");
print("PDO::FETCH_OBJ: ");
print("Return next row as an anonymous object with column names as properties\n");
$result = $sth->fetch(PDO::FETCH_OBJ);
print $result->NAME;
print("\n");
?>
�������̻������
PDO::FETCH_ASSOC: Return next row as an array indexed by column name Array ( [NAME] => apple [COLOUR] => red ) PDO::FETCH_BOTH: Return next row as an array indexed by both column name and number Array ( [NAME] => banana [0] => banana [COLOUR] => yellow [1] => yellow ) PDO::FETCH_LAZY: Return next row as an anonymous object with column names as properties PDORow Object ( [NAME] => orange [COLOUR] => orange ) PDO::FETCH_OBJ: Return next row as an anonymous object with column names as properties kiwi
Example #2 ʹ��һ���ɹ����α��ȡ��
<?php
function readDataForwards($dbh) {
$sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY BET';
try {
$stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_NEXT)) {
$data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
print $data;
}
$stmt = null;
}
catch (PDOException $e) {
print $e->getMessage();
}
}
function readDataBackwards($dbh) {
$sql = 'SELECT hand, won, bet FROM mynumbers ORDER BY bet';
try {
$stmt = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_LAST);
do {
$data = $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
print $data;
} while ($row = $stmt->fetch(PDO::FETCH_NUM, PDO::FETCH_ORI_PRIOR));
$stmt = null;
}
catch (PDOException $e) {
print $e->getMessage();
}
}
print "Reading forwards:\n";
readDataForwards($conn);
print "Reading backwards:\n";
readDataBackwards($conn);
?>
�������̻������
Reading forwards: 21 10 5 16 0 5 19 20 10 Reading backwards: 19 20 10 16 0 5 21 10 5