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 )
Use PDOStatement::columnCount() to return 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 determined until after PDOStatement::execute() is called.
Return Value
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
Counting 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");
/* Count the number of columns in a (non-existent) result set */
$colcount = $sth->columnCount();
print("Before execute(), result set has $colcount columns (should be 0)\n");
$sth->execute();
/* Count the number of columns in the result set */
$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)