PDOStatement::columnCount
PDOStatement::columnCount — Returns the number of columns in the result set. (PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)
Description
Syntax
int PDOStatement::columnCount ( void )
PDOStatement::columnCount() returns the number of columns in the result set represented by the PDOStatement object.
If the PDOStatement object was returned by PDO::query(), the column count is immediately available.
If the PDOStatement object was returned by PDO::prepare(), the column count cannot be accurately calculated until PDOStatement::execute() is called.
Return Values
Returns the number of columns in the result set represented by the PDOStatement object. If there is no result set, PDOStatement::columnCount() returns 0.
Examples
Count columns
The following example demonstrates how to use PDOStatement::columnCount() with a result set and an empty set.
<?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");
?>
The above example will output:
Before execute(), result set has 0 columns (should be 0) After execute(), result set has 2 columns (should be 2)
Other Extensions
PHP PDO Reference Manual