PDOStatement::bindColumn
PDOStatement::bindColumn — 绑定一列到一个 PHP 变量(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)
说明
语法
bool PDOStatement::bindColumn ( mixed $column , mixed &$param [, int $type [, int $maxlen [, mixed $driverdata ]]] )
安排一个特定的变量绑定到一个查询结果集中给定的列。每次调用 PDOStatement::fetch() 或 PDOStatement::fetchAll() 都将更新所有绑定到列的变量。
注意:在语句执行前 PDO 有关列的信息并非总是可用,可移植的应用应在 PDOStatement::execute() 之后 调用此函数(方法)。
但是,当使用 PgSQL 驱动 时,要想能绑定一个 LOB 列作为流,应用程序必须在调用 PDOStatement::execute() 之前 调用此方法,否则大对象 OID 作为一个整数返回。
参数
column
结果集中的列号(从1开始索引)或列名。如果使用列名,注意名称应该与由驱动返回的列名大小写保持一致。
param
将绑定到列的 PHP 变量名称
type
通过 PDO::PARAM_* 常量指定的参数的数据类型。
maxlen
预分配提示。
driverdata
驱动的可选参数。
返回值
成功时返回 TRUE, 或者在失败时返回 FALSE。
实例
把结果集输出绑定到 PHP 变量
绑定结果集中的列到PHP变量是一种使每行包含的数据在应用程序中立即可用的有效方法。下面的例子演示了 PDO 怎样用多种选项和缺省值绑定和检索列。
<?php
function readData($dbh) {
$sql = 'SELECT name, colour, calories FROM fruit';
try {
$stmt = $dbh->prepare($sql);
$stmt->execute();
/* 通过列号绑定 */
$stmt->bindColumn(1, $name);
$stmt->bindColumn(2, $colour);
/* 通过列名绑定 */
$stmt->bindColumn('calories', $cals);
while ($row = $stmt->fetch(PDO::FETCH_BOUND)) {
$data = $name . "\t" . $colour . "\t" . $cals . "\n";
print $data;
}
}
catch (PDOException $e) {
print $e->getMessage();
}
}
readData($dbh);
?>
以上例程会输出:
apple red 150 banana yellow 175 kiwi green 75 orange orange 150 mango red 200 strawberry red 25
点击查看所有 PHP 教程 文章: https://www.codercto.com/courses/l/5.html
PHP for the World Wide Web, Second Edition (Visual QuickStart Gu
Larry Ullman / Peachpit Press / 2004-02-02 / USD 29.99
So you know HTML, even JavaScript, but the idea of learning an actual programming language like PHP terrifies you? Well, stop quaking and get going with this easy task-based guide! Aimed at beginning ......一起来看看 《PHP for the World Wide Web, Second Edition (Visual QuickStart Gu》 这本书的介绍吧!
图片转BASE64编码
在线图片转Base64编码工具
HEX CMYK 转换工具
HEX CMYK 互转工具