YouTip LogoYouTip

Pdostatement Columncount

PDOStatement::columnCount |

-- Learning is not just about technology, but also about dreams!

PHP Tutorial

PHP Forms

PHP Advanced

PHP 7 New Features

PHP Database

PHP XML

PHP and AJAX

PHP Reference

PHP 5 MySQLi Functions

PHP 5 SimpleXML Functions

Explore Further

  • Web Service
  • Programming
  • Development Tools
  • Network Services
  • Scripting Languages
  • Software
  • Computer Science
  • Web Design & Development
  • Programming Languages
  • Scripts

PDOStatement::columnCount

PHP PDO ReferencePHP PDO Reference

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 PDOStatement::execute() is called.


Return Values

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() on 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)

PHP PDO ReferencePHP PDO Reference

← Pdostatement DebugdumpparamsPdostatement Closecursor β†’