PDOStatement->fetchColumn()
(PHP 5 >= 5.1.0, PECL pdo:0.9-1.0.3)
PDOStatement->fetchColumn() — 結果セットの次行から単一カラムを返す
説明
PDOStatement
string fetchColumn
([ int $column_number
] )
結果セットの次行から単一カラムを返します。 行がもうない場合には FALSE を返します。
パラメータ
- column_number
-
行から処理したい 0 で始まるカラム番号を指定します。 何も値が与えられない場合、 PDOStatement->fetchColumn() は最初のカラムをフェッチします。
返り値
PDOStatement->fetchColumn() は結果セットの次行から単一カラムを返します。
警告
PDOStatement->fetchColumn() を使用してデータを処理する場合、同一行から 他のカラムを返す方法はありません。
例
Example#1 次行の最初のカラムを返す
<?php
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
$sth->execute();
/* 結果セットの次行から最初のカラムをフェッチする */
print("Fetch the first column from the next row in the result set:\n");
$result = $sth->fetchColumn();
print("name = $result\n");
print("Fetch the second column from the next row in the result set:\n");
$result = $sth->fetchColumn(1);
print("colour = $result\n");
?>
上の例の出力は以下となります。
Fetch the first column from the next row in the result set: name = lemon Fetch the second column from the next row in the result set: colour = red