PDOStatement::columnCount
PDOStatement::columnCount — 結果セット内の列数を返す。(PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)
説明
構文
int PDOStatement::columnCount ( void )
PDOStatement::columnCount() を使用して、PDOStatement オブジェクトが表す結果セット内の列数を返します。
PDO::query() によって返された PDOStatement オブジェクトの場合、列数の計算は直ちに利用可能です。
PDO::prepare() によって返された PDOStatement オブジェクトの場合、PDOStatement::execute() を呼び出す前は列数を正確に計算できません。
戻り値
PDOStatement オブジェクトが表す結果セット内の列数を返します。結果セットがない場合、PDOStatement::columnCount() は 0 を返します。
例
列数の計算
次の例は、PDOStatement::columnCount() を使用して結果セットと空のセットを操作する方法を示しています。
<?php
$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
/* 计算一个(不存在)的结果集中的列数 */
$colcount = $sth->columnCount();
print("Before execute(), result set has $colcount columns (should be 0)\n");
$sth->execute();
/* 计算结果集中的列数 */
$colcount = $sth->columnCount();
print("After execute(), result set has $colcount columns (should be 2)\n");
?>
上の例の出力は次のようになります:
Before execute(), result set has 0 columns (should be 0) After execute(), result set has 2 columns (should be 2)
その他の拡張モジュール
PHP PDO リファレンスマニュアル