PDOStatement::fetch

PHP PDO 參考手冊PHP PDO 參考手冊

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 的值 (默認(rèn)為 PDO::FETCH_BOTH )。

  • PDO::FETCH_ASSOC:返回一個索引為結(jié)果集列名的數(shù)組

  • PDO::FETCH_BOTH(默認(rèn)):返回一個索引為結(jié)果集列名和以0開始的列號的數(shù)組

  • PDO::FETCH_BOUND:返回 TRUE ,并分配結(jié)果集中的列值給PDOStatement::bindColumn() 方法綁定的 PHP 變量。

  • PDO::FETCH_CLASS:返回一個請求類的新實(shí)例,映射結(jié)果集中的列名到類中對應(yīng)的屬性名。如果 fetch_style 包含 PDO::FETCH_CLASSTYPE(例如:PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE),則類名由第一列的值決定

  • PDO::FETCH_INTO:更新一個被請求類已存在的實(shí)例,映射結(jié)果集中的列到類中命名的屬性

  • PDO::FETCH_LAZY:結(jié)合使用 PDO::FETCH_BOTHPDO::FETCH_OBJ,創(chuàng)建供用來訪問的對象變量名

  • PDO::FETCH_NUM:返回一個索引為以0開始的結(jié)果集列號的數(shù)組

  • PDO::FETCH_OBJ:返回一個屬性名對應(yīng)結(jié)果集列名的匿名對象

cursor_orientation
對于 一個 PDOStatement 對象表示的可滾動游標(biāo),該值決定了哪一行將被返回給調(diào)用者。此值必須是 PDO::FETCH_ORI_* 系列常量中的一個,默認(rèn)為 PDO::FETCH_ORI_NEXT。要想讓 PDOStatement 對象使用可滾動游標(biāo),必須在用 PDO::prepare() 預(yù)處理SQL語句時,設(shè)置 PDO::ATTR_CURSOR 屬性為 PDO::CURSOR_SCROLL。

offset
對于一個 cursor_orientation 參數(shù)設(shè)置為 PDO::FETCH_ORI_ABS 的PDOStatement 對象代表的可滾動游標(biāo),此值指定結(jié)果集中想要獲取行的絕對行號。
對于一個 cursor_orientation 參數(shù)設(shè)置為 PDO::FETCH_ORI_REL 的PDOStatement 對象代表的可滾動游標(biāo),此值指定想要獲取行相對于調(diào)用 PDOStatement::fetch() 前游標(biāo)的位置


返回值

此函數(shù)(方法)成功時返回的值依賴于提取類型。在所有情況下,失敗都返回 FALSE 。


實(shí)例

用不同的提取方式獲取行

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

以上實(shí)例會輸出:

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

使用一個可滾動游標(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);
?>

以上實(shí)例會輸出:

Reading forwards:
21    10    5
16    0     5
19    20    10

Reading backwards:
19    20    10
16    0     5
21    10    5

PHP PDO 參考手冊PHP PDO 參考手冊