欢迎来到站长教程网!
兔子CDN

PHP教程

当前位置:主页 > 网络编程 > PHP教程 >

PHP PDOStatement::fetch讲解

时间:2020-10-18|栏目:PHP教程|点击:

PDOStatement::fetch

PDOStatement::fetch — 从结果集中获取下一行(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 对象相关的结果集中获取下一行。fetch_style 参数决定 POD 如何返回行。

参数

fetch_style

控制下一行如何返回给调用者。此值必须是 PDO::FETCH_* 系列常量中的一个,缺省为 PDO::ATTR_DEFAULT_FETCH_MODE 的值 (默认为 PDO::FETCH_BOTH )。

cursor_orientation

offset

返回值

此函数(方法)成功时返回的值依赖于提取类型。在所有情况下,失败都返回 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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对本网站的支持。如果你想了解更多相关内容请查看下面相关链接

您可能感兴趣的文章:

上一篇:PHP使用mongoclient简单操作mongodb数据库示例

栏    目:PHP教程

下一篇:Laravel框架实现超简单的分页效果示例

本文标题:PHP PDOStatement::fetch讲解

本文地址:www.dtcnnet.com/PHPjiaocheng/43294.html

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:584415406 | 邮箱:584415406#qq.com(#换成@)

Copyright © 2015-2020 小白站长网 版权所有 苏ICP备20040415号