PHP PDOStatement:: columnCount Explanation

  • 2021-11-14 05:11:48
  • OfStack

PDOStatement::columnCount

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

Description

Grammar


int PDOStatement::columnCount ( void )

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

If it is by PDO::query() PDOStatement object returned, column count calculation is immediately available.

If it is by PDO::prepare() The PDOStatement object returned by the PDOStatement::execute() Before, the number of columns could not be calculated accurately.

Return value

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

Instances

Calculate the number of columns

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


<?php
$dbh = new PDO('odbc:sample', 'db2inst1', 'ibmdb2');
$sth = $dbh->prepare("SELECT name, colour FROM fruit");
/*  Calculation 1 Number of columns in the result set (non-existent)  */
$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 routine outputs:

Before execute(), result set has 0 columns (should be 0)
After execute(), result set has 2 columns (should be 2)

Summarize


Related articles: