PDOStatement::fetch
PDOStatement::fetch — 從結(jié)果集中獲取下一行(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
說明
語法
mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )
從一個 PDOStatement 對象相關(guān)的結(jié)果集中獲取下一行。fetch_style 參數(shù)決定 POD 如何返回行。
參數(shù)
fetch_style
控制下一行如何返回給調(diào)用者。此值必須是 PDO::FETCH_* 系列常量中的一個,缺省為 PDO::ATTR_DEFAULT_FETCH_MODE 的值 (默認為 PDO::FETCH_BOTH )。
- _PDO::FETCHASSOC :返回一個索引為結(jié)果集列名的數(shù)組
- _PDO::FETCHBOTH (默認):返回一個索引為結(jié)果集列名和以0開始的列號的數(shù)組
- _PDO::FETCHBOUND :返回 TRUE ,并分配結(jié)果集中的列值給PDOStatement::bindColumn() 方法綁定的 PHP 變量。
- _PDO::FETCHCLASS :返回一個請求類的新實例,映射結(jié)果集中的列名到類中對應的屬性名。如果 fetch_style 包含 PDO::FETCH_CLASSTYPE(例如: _PDO::FETCH_CLASS | PDO::FETCHCLASSTYPE ),則類名由第一列的值決定
- _PDO::FETCHINTO :更新一個被請求類已存在的實例,映射結(jié)果集中的列到類中命名的屬性
- _PDO::FETCHLAZY :結(jié)合使用 _PDO::FETCHBOTH 和 _PDO::FETCHOBJ ,創(chuàng)建供用來訪問的對象變量名
- _PDO::FETCHNUM :返回一個索引為以0開始的結(jié)果集列號的數(shù)組
- _PDO::FETCHOBJ :返回一個屬性名對應結(jié)果集列名的匿名對象
cursor_orientation
- 對于 一個 PDOStatement 對象表示的可滾動游標,該值決定了哪一行將被返回給調(diào)用者。此值必須是 PDO::FETCHORI* 系列常量中的一個,默認為 PDO::FETCH_ORI_NEXT。要想讓 PDOStatement 對象使用可滾動游標,必須在用 PDO::prepare() 預處理SQL語句時,設置 PDO::ATTR_CURSOR 屬性為 PDO::CURSOR_SCROLL。
offset
- 對于一個 cursor_orientation 參數(shù)設置為 PDO::FETCH_ORI_ABS 的PDOStatement 對象代表的可滾動游標,此值指定結(jié)果集中想要獲取行的絕對行號。
- 對于一個 cursor_orientation 參數(shù)設置為 PDO::FETCH_ORI_REL 的PDOStatement 對象代表的可滾動游標,此值指定想要獲取行相對于調(diào)用 PDOStatement::fetch() 前游標的位置
返回值
此函數(shù)(方法)成功時返回的值依賴于提取類型。在所有情況下,失敗都返回 FALSE 。
實例
用不同的提取方式獲取行
?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
使用一個可滾動游標獲取行
?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
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
您可能感興趣的文章:- PHP PDOStatement::setFetchMode講解
- PHP PDOStatement::setAttribute講解
- PHP PDOStatement::rowCount講解
- PHP PDOStatement::nextRowset講解
- PHP PDOStatement::getColumnMeta講解
- PHP PDOStatement::getAttribute講解
- PHP PDOStatement::fetchObject講解
- PHP PDOStatement::fetchColumn講解
- PHP PDOStatement::fetchAll講解
- PHP _construct()函數(shù)講解