downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/pdostatement.columncount.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/class.pdostatement.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'pdostatement.columncount.php',
   
1 => 'PDOStatement->columnCount',
  ),
 
'up' =>
  array (
   
0 => 'class.pdostatement.php',
   
1 => 'The PDOStatement class',
  ),
 
'prev' =>
  array (
   
0 => 'pdostatement.closecursor.php',
   
1 => 'PDOStatement->closeCursor',
  ),
 
'next' =>
  array (
   
0 => 'pdostatement.debugdumpparams.php',
   
1 => 'PDOStatement->debugDumpParams',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="pdostatement.columncount" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement-&gt;columnCount</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PECL pdo &gt;= 0.2.0)</p><p class="refpurpose"><span class="refname">PDOStatement-&gt;columnCount</span> &mdash; <span class="dc-title">
   Returns the number of columns in the result set
  </span></p>

 </div>
 <a name="pdostatement.columncount.description"></a><div class="refsect1 description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">int</span> <span class="methodname"><b>PDOStatement::columnCount</b></span>
    ( <span class="methodparam">void</span>
   )</div>


  <p class="para rdfs-comment">
   Use <b>PDOStatement::columnCount()</b> to return the number
   of columns in the result set represented by the PDOStatement object.
  </p>
  <p class="para">
   If the PDOStatement object was returned from <a href="pdo.query.php" class="function">PDO::query()</a>,
   the column count is immediately available.
  </p>
  <p class="para">
   If the PDOStatement object was returned from
   <a href="pdo.prepare.php" class="function">PDO::prepare()</a>, an accurate column count will not be
   available until you invoke <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>.
  </p>

 </div>

 <a name="pdostatement.columncount.returnvalues"></a><div class="refsect1 returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the number of columns in the result set represented by the
   PDOStatement object. If there is no result set,
   <b>PDOStatement::columnCount()</b> returns <i>0</i>.
  </p>
 </div>


 <a name="pdostatement.columncount.examples"></a><div class="refsect1 examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   </p><div class="example">
    <p><b>Example #1 Counting columns</b></p>
    <div class="example-contents para"><p>
     This example demonstrates how <b>PDOStatement::columnCount()</b>
     operates with and without a result set.
    </p></div>
    <div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$dbh&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'odbc:sample'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'db2inst1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'ibmdb2'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sth&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;name,&nbsp;colour&nbsp;FROM&nbsp;fruit"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;Count&nbsp;the&nbsp;number&nbsp;of&nbsp;columns&nbsp;in&nbsp;the&nbsp;(non-existent)&nbsp;result&nbsp;set&nbsp;*/<br /></span><span style="color: #0000BB">$colcount&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">columnCount</span><span style="color: #007700">();<br />print(</span><span style="color: #DD0000">"Before&nbsp;execute(),&nbsp;result&nbsp;set&nbsp;has&nbsp;</span><span style="color: #0000BB">$colcount</span><span style="color: #DD0000">&nbsp;columns&nbsp;(should&nbsp;be&nbsp;0)\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/*&nbsp;Count&nbsp;the&nbsp;number&nbsp;of&nbsp;columns&nbsp;in&nbsp;the&nbsp;result&nbsp;set&nbsp;*/<br /></span><span style="color: #0000BB">$colcount&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$sth</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">columnCount</span><span style="color: #007700">();<br />print(</span><span style="color: #DD0000">"After&nbsp;execute(),&nbsp;result&nbsp;set&nbsp;has&nbsp;</span><span style="color: #0000BB">$colcount</span><span style="color: #DD0000">&nbsp;columns&nbsp;(should&nbsp;be&nbsp;2)\n"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents para"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Before execute(), result set has 0 columns (should be 0)
After execute(), result set has 2 columns (should be 2)
</pre></div>
    </div>
   </div><p>
  </p>
 </div>



 <a name="pdostatement.columncount.seealso"></a><div class="refsect1 seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   </p><ul class="simplelist">
    <li class="member"><a href="pdo.prepare.php" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</li>
    <li class="member"><a href="pdostatement.execute.php" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</li>
    <li class="member"><a href="pdostatement.rowcount.php" class="function" rel="rdfs-seeAlso">PDOStatement::rowCount()</a> - Returns the number of rows affected by the last SQL statement</li>
   </ul><p>
  </p>
 </div>


</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites