Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PDOStatement::columnCount

PDOStatement::columnCount

PHP PDO Reference ManualPHP PDO Reference Manual

PDOStatement::columnCount - Return 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 it is a PDOStatement object returned by PDO::query(), the column count is available immediately.

If it is a PDOStatement object returned by PDO::prepare(), the column count cannot be accurately computed until 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.


Example

Count the columns

The following example demonstrates how to use PDOStatement::columnCount() with a result set and an empty set.



prepare("SELECT name, colour FROM fruit");



/* Calculate 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();



/* 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)


PHP PDO Reference ManualPHP PDO Reference Manual