PDOStatement::fetch
(PHP 5 >= 5.1.0, PHP 7, PECL pdo >= 0.1.0)
PDOStatement :: fetch - 从结果集中获取下一行
描述
public mixed PDOStatement::fetch ([ int $fetch_style [, int $cursor_orientation = PDO::FETCH_ORI_NEXT [, int $cursor_offset = 0 ]]] )
从与PDOStatement对象关联的结果集中获取一行。该fetch_style
参数确定PDO如何返回该行。
参数
fetch_style
控制下一行将如何返回给调用者。该值必须是PDO :: FETCH_ *常量之一,默认为PDO :: ATTR_DEFAULT_FETCH_MODE(默认为PDO :: FETCH_BOTH)的值。
- PDO :: FETCH_ASSOC:返回按结果集里返回的列名索引的数组
- PDO :: FETCH_BOTH(默认值):返回由结果集里返回的列名和索引列号索引的数组
- PDO :: FETCH_BOUND:返回
TRUE
并将结果集里列的值赋给它们绑定到的PDOStatement :: bindColumn()方法所绑定的PHP变量
- PDO :: FETCH_CLASS:返回请求类的新实例,将结果集的列映射到类中的命名属性,然后调用构造函数,除非还提供了PDO :: FETCH_PROPS_LATE。如果
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_NAMED:返回一个具有与PDO :: FETCH_ASSOC相同形式的数组,除了这种情况:如果有多个具有相同名称的列,则该键引用的值将是该行所有值的数组该列名称
- PDO :: FETCH_NUM:返回按结果集中返回的列号索引的数组,从第0列开始
- PDO :: FETCH_OBJ:返回一个匿名对象,其属性名称对应于结果集中返回的列名称
- PDO :: FETCH_PROPS_LATE:与PDO :: FETCH_CLASS一起使用时,在从各个列值分配属性之前调用该类的构造函数。
cursor_orientation
对于表示可滚动游标的PDOStatement对象,此值确定将哪个行返回给调用者。该值必须是PDO :: FETCH_ORI_ *常量之一,默认为PDO :: FETCH_ORI_NEXT。要请求您的PDOStatement对象物体滚动游标,在您准备SQL语句PDO::prepare()时,您必须设置PDO :: ATTR_CURSOR属性PDO :: CURSOR_SCROLL。
offset
对于表示可设置cursor_orientation参数为PDO :: FETCH_ORI_ABS的可滚动游标的PDOStatement对象,此值指定了将被提取的结果集中行的绝对编号。
对于表示可滚动游标的PDOStatement对象,其cursor_orientation参数设置为PDO :: FETCH_ORI_REL,该值指定在调用PDOStatement :: fetch()之前相对于游标位置提取的行。
返回值
成功时此函数的返回值取决于提取类型。在所有情况下,都会在失败时返回FALSE
。
示例
示例#1 使用不同的获取样式获取行
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* Exercise PDOStatement::fetch styles */
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
示例#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
示例#3 构造顺序
当通过PDO :: FETCH_CLASS获取对象时,首先分配对象属性,然后调用该类的构造函数。如果还提供了PDO :: FETCH_PROPS_LATE,则顺序相反,即首先调用构造函数,然后分配属性。
<?php
class Person
{
private $name;
public function __construct()
{
$this->tell();
}
public function tell()
{
if (isset($this->name)) {
echo "I am {$this->name}.\n";
} else {
echo "I don't have a name yet.\n";
}
}
}
$sth = $dbh->query("SELECT * FROM people");
$sth->setFetchMode(PDO::FETCH_CLASS, 'Person');
$person = $sth->fetch();
$person->tell();
$sth->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, 'Person');
$person = $sth->fetch();
$person->tell();
?>
上面的例子会输出如下信息:
I am Alice.
I am Alice.
I don't have a name yet.
I am Bob.
扩展内容
- PDO :: prepare() - 准备执行语句并返回一个语句对象
- PDOStatement :: execute() - 执行准备好的语句
- PDOStatement :: fetchAll() - 返回一个包含所有结果集行的数组
- PDOStatement :: fetchColumn() - 返回结果集的下一行中的单个列
- PDOStatement :: fetchObject() - 获取下一行并将其作为对象返回。
- PDOStatement :: setFetchMode() - 为此语句设置默认的获取模式
← PDOStatement::execute
PDOStatement::fetchAll →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com