PHP PDOStatement::setFetchMode讲解

PDOStatement::setFetchMode

PDOStatement::setFetchMode — 为语句设置默认的获取模式。(PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)

说明

语法

bool PDOStatement::setFetchMode ( int $mode )
bool PDOStatement::setFetchMode ( int $PDO::FETCH_COLUMN , int $colno )
bool PDOStatement::setFetchMode ( int $PDO::FETCH_CLASS , string $classname , array $ctorargs )
bool PDOStatement::setFetchMode ( int $PDO::FETCH_INTO , object $object )

参数

mode

  • 获取模式必须是 PDO::FETCH_* 系列常量中的一个。

colno

  • 列号。

classname

  • 类名。

ctorargs

  • 构造函数参数。

object

  • 对象。

返回值

成功时返回 TRUE, 或者在失败时返回 FALSE。

实例

设置获取模式

下面的例子示范如何用 PDOStatement::setFetchMode() 来为一个 PDOStatement 对象更改默认的获取模式。

<?php
$sql = 'SELECT name, colour, calories FROM fruit';
try {
 $stmt = $dbh->query($sql);
 $result = $stmt->setFetchMode(PDO::FETCH_NUM);
 while ($row = $stmt->fetch()) {
  print $row[0] . "\t" . $row[1] . "\t" . $row[2] . "\n";
 }
}
catch (PDOException $e) {
 print $e->getMessage();
}
?>

以上实例输出为:

apple   red     150
banana  yellow  250
orange  orange  300
kiwi    brown   75
lemon   yellow  25
pear    green   150
watermelon      pink    90

总结

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

(0)

相关推荐

  • PHP getNamespaces()函数讲解

    PHP getNamespaces() 函数 实例 返回 XML 文档中使用的命名空间: <?php $xml=<<<XML <?xml version="1.0" standalone="yes"?> <cars xmlns:c="http://codingdict.com/ns" xmlns:a="http://codingdict.com/country"> <c:c

  • PHP _construct()函数讲解

    PHP _construct() 函数 实例 函数创建一个新的 SimpleXMLElement 对象,然后输出 body 节点的内容: <?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</

  • PHP attributes()函数讲解

    PHP attributes() 函数 实例 返回 XML 的 body 元素的属性和值: <?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body date="2013-01-01" type="private">Don'

  • PHP getDocNamespaces()函数讲解

    PHP getDocNamespaces() 函数 实例 返回 XML 文档的根节点中声明的命名空间: <?php $xml=<<<XML <?xml version="1.0" standalone="yes"?> <cars xmlns:c="http://codingdict.com/ns"> <c:car id="1">Volvo</c:car>

  • PHP asXML()函数讲解

    PHP asXML()函数 实例 格式化 XML(版本 1.0)中的 SimpleXML 对象的数据: <?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </not

  • PHP addAttribute()函数讲解

    PHP addAttribute() 函数 实例 给根元素和 body 元素添加一个属性: <?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

  • PHP getName()函数讲解

    PHP getName() 函数 实例 返回 XML 元素及其子元素的名称: <?php $xml=<<<XML <?xml version="1.0" standalone="yes"?> <cars> <car id="1">Volvo</car> <car id="2">BMW</car> <car id="3

  • PHP count()函数讲解

    PHP count() 函数 实例 计算 car 节点的子节点个数: <?php $xml=<<<XML <cars> <car name="Volvo"> <child/> <child/> <child/> <child/> </car> <car name="BMW"> <child/> <child/> </c

  • PHP registerXPathNamespace()函数讲解

    PHP registerXPathNamespace() 函数 实例 为下一个 XPath 查询创建命名空间上下文: <?php $xml=<<<XML <book xmlns:chap="http://example.org/chapter-title"> <title>My Book</title> <chapter id="1"> <chap:title>Chapter 1&l

  • PHP children()函数讲解

    PHP children() 函数 实例 查找 note 节点的子节点: <?php $note=<<<XML <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> XML; $xml

随机推荐