Latest web development tutorials

PDOStatement::fetchColumn

PHP PDO 參考手冊 PHP PDO參考手冊

PDOStatement::fetchColumn — 從結果集中的下一行返回單獨的一列。 (PHP 5 >= 5.1.0, PECL pdo >= 0.9.0)


說明

語法

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

從結果集中的下一行返回單獨的一列,如果沒有了,則返回FALSE 。


參數

column_number

你想從行里取回的列的索引數字(以0開始的索引)。 如果沒有提供值,則PDOStatement::fetchColumn() 獲取第一列。


返回值

PDOStatement::fetchColumn() 從結果集中的下一行返回單獨的一列。

注意: 如果使用PDOStatement::fetchColumn()取回數據,則沒有辦法返回同一行的另外一列。


實例

返回下一行的第一列

<?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");
?>

以上實例會輸出:

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

PHP PDO 參考手冊 PHP PDO參考手冊