= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)PDOStatement::fetch — 從結(jié)果集中獲取下一行 說(shuō)明PDOStatement::fetch(int $fetch_style = ?, int $">
(PHP 5 >= 5.1.0, PHP 7, PHP 8, PECL pdo >= 0.1.0)
PDOStatement::fetch — 從結(jié)果集中獲取下一行
$fetch_style
= ?, int $cursor_orientation
= PDO::FETCH_ORI_NEXT, int $cursor_offset
= 0): mixed
從一個(gè) PDOStatement 對(duì)象相關(guān)的結(jié)果集中獲取下一行。fetch_style
參數(shù)決定 POD 如何返回行。
fetch_style
控制下一行如何返回給調(diào)用者。此值必須是 PDO::FETCH_*
系列常量中的一個(gè),缺省為 PDO::ATTR_DEFAULT_FETCH_MODE
的值 (默認(rèn)為 PDO::FETCH_BOTH
)。
PDO::FETCH_ASSOC
:返回一個(gè)索引為結(jié)果集列名的數(shù)組
PDO::FETCH_BOTH
(默認(rèn)):返回一個(gè)索引為結(jié)果集列名和以0開始的列號(hào)的數(shù)組
PDO::FETCH_BOUND
:返回 true
,并分配結(jié)果集中的列值給 PDOStatement::bindColumn() 方法綁定的 PHP 變量。
PDO::FETCH_CLASS
:返回一個(gè)請(qǐng)求類的新實(shí)例,映射結(jié)果集中的列名到類中對(duì)應(yīng)的屬性名。如果 fetch_style
包含 PDO::FETCH_CLASSTYPE(例如:PDO::FETCH_CLASS |
PDO::FETCH_CLASSTYPE
),則類名由第一列的值決定
PDO::FETCH_INTO
:更新一個(gè)被請(qǐng)求類已存在的實(shí)例,映射結(jié)果集中的列到類中命名的屬性
PDO::FETCH_LAZY
:結(jié)合使用 PDO::FETCH_BOTH
和 PDO::FETCH_OBJ
,創(chuàng)建供用來(lái)訪問(wèn)的對(duì)象變量名
PDO::FETCH_NUM
:返回一個(gè)索引為以0開始的結(jié)果集列號(hào)的數(shù)組
PDO::FETCH_OBJ
:返回一個(gè)屬性名對(duì)應(yīng)結(jié)果集列名的匿名對(duì)象
cursor_orientation
對(duì)于 一個(gè) PDOStatement 對(duì)象表示的可滾動(dòng)游標(biāo),該值決定了哪一行將被返回給調(diào)用者。此值必須是 PDO::FETCH_ORI_*
系列常量中的一個(gè),默認(rèn)為 PDO::FETCH_ORI_NEXT
。要想讓 PDOStatement 對(duì)象使用可滾動(dòng)游標(biāo),必須在用 PDO::prepare() 預(yù)處理SQL語(yǔ)句時(shí),設(shè)置 PDO::ATTR_CURSOR
屬性為 PDO::CURSOR_SCROLL
。
offset
對(duì)于一個(gè) cursor_orientation
參數(shù)設(shè)置為 PDO::FETCH_ORI_ABS
的PDOStatement 對(duì)象代表的可滾動(dòng)游標(biāo),此值指定結(jié)果集中想要獲取行的絕對(duì)行號(hào)。
對(duì)于一個(gè) cursor_orientation
參數(shù)設(shè)置為 PDO::FETCH_ORI_REL
的PDOStatement 對(duì)象代表的可滾動(dòng)游標(biāo),此值指定想要獲取行相對(duì)于調(diào)用 PDOStatement::fetch() 前游標(biāo)的位置
此函數(shù)(方法)成功時(shí)返回的值依賴于提取類型。在所有情況下,失敗都返回 false
。
示例 #1 使用不同的提取方式獲取行
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* 運(yùn)用 PDOStatement::fetch 風(fēng)格 */
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");
?>
以上例程會(huì)輸出:
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
示例 #2 使用一個(gè)可滾動(dòng)游標(biāo)獲取行
<?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);
?>
以上例程會(huì)輸出:
Reading forwards: 21 10 5 16 0 5 19 20 10 Reading backwards: 19 20 10 16 0 5 21 10 5