最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
PHP PDOStatementbindColumn讲解
时间:2026-05-29 19:00:02 编辑:袖梨 来源:一聚教程网
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
param
type
maxlen
driverdata
返回值
成功时返回 TRUE, 或者在失败时返回 FALSE。
实例
把结果集输出绑定到 PHP 变量
绑定结果集中的列到PHP变量是一种使每行包含的数据在应用程序中立即可用的有效方法。下面的例子演示了 PDO 怎样用多种选项和缺省值绑定和检索列。
<?phpfunction 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 150banana yellow 175kiwi green 75orange orange 150mango red 200strawberry red 25
总结