PDOStatement::fetchColumn

PHP PDO 参考手册PHP PDO Reference Manual

PDOStatement::fetchColumn — Returns a single column from the next row of a result set. (PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)


Description

Syntax

string PDOStatement::fetchColumn ([ int $column_number = 0 ] )

Returns a single column from the next row of a result set, or FALSE if there are no more rows.


Parameters

column_number

The column number you wish to retrieve from the row. The first column is 0. If no value is specified, PDOStatement::fetchColumn() fetches the first column.


Return Values

PDOStatement::fetchColumn() returns a single column from the next row of a result set.

Note:If you use PDOStatement::fetchColumn() to retrieve data, there is no way to return another column from the same row.


Examples

Return the first column of the next row

<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();

/* 从结果集中的下一行获取第一列  */
print("从结果集中的下一行获取第一列:\n");
$result = $sth->fetchColumn();
print("name = $result\n");

print("从结果集中的下一行获取第二列:\n");
$result = $sth->fetchColumn(1);
print("colour = $result\n");
?>

The above example will output:

从结果集中的下一行获取第一列:
name = lemon
从结果集中的下一行获取第二列:
colour = red

PHP PDO 参考手册PHP PDO Reference Manual

Other Extensions