PDOStatement::columnCount — Returns the number of columns in the result set. (PHP 5 >= 5.1.0, PECL pdo >= 0.2.0)
int PDOStatement::columnCount ( void )
Use PDOStatement::columnCount() to return the number of columns in the result set represented by the PDOStatement object.
In the case of a PDOStatement object returned by PDO::query(), column count calculations are available immediately.
If it is a PDOStatement object returned by PDO::prepare(), the number of columns cannot be accurately calculated until PDOStatement::execute() is called.
Returns the number of columns in the result set represented by the PDOStatement object. If there is no result set, PDOStatement::columnCount() returns 0.
The following example demonstrates how to use PDOStatement::columnCount() to operate on a result set and an empty set.
<?php$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');$sth = $dbh->prepare("SELECT name, color FROM fruit");/* Calculate one (does not exist )The number of columns in the result set*/$colcount = $sth->columnCount();print("Before execute(), result set has $colcount columns (should be 0)n");$sth->execute();/* Calculate 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 routine will output:
Before execute(), result set has 0 columns (should be 0)After execute(), result set has 2 columns (should be 2)