Latest web development tutorials

PDOStatement :: columnCount

PHP PDO Reference Manual PHP PDO Reference Manual

PDOStatement :: columnCount - Returns the number of columns in the result set. (PHP 5> = 5.1.0, PECL pdo> = 0.2.0)


Explanation

grammar

int PDOStatement::columnCount ( void )

Use PDOStatement :: columnCount () Returns the number of columns in the result by the PDOStatement object representing the set.

If the PDOStatement object is returned by the PDO :: query (), then calculate the number of columns available immediately.

If the PDO :: prepare () returns a PDOStatement object, calling PDOStatement :: execute can not accurately calculate the number of columns (before).


return value

Returns the number of columns by the PDOStatement object represents the result set. If there is no result set, PDOStatement :: columnCount () returns 0.


Examples

The number of computed columns

The following example shows how to use PDOStatement :: columnCount () operation 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)

PHP PDO Reference Manual PHP PDO Reference Manual