<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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.rowcount.php',
    1 => 'PDOStatement::rowCount',
    2 => 'Returns the number of rows affected by the last SQL statement',
  ),
  'up' => 
  array (
    0 => 'class.pdostatement.php',
    1 => 'PDOStatement',
  ),
  'prev' => 
  array (
    0 => 'pdostatement.nextrowset.php',
    1 => 'PDOStatement::nextRowset',
  ),
  'next' => 
  array (
    0 => 'pdostatement.setattribute.php',
    1 => 'PDOStatement::setAttribute',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo/pdostatement/rowcount.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdostatement.rowcount" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDOStatement::rowCount</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement::rowCount</span> &mdash; <span class="dc-title">
   Returns the number of rows affected by the last SQL statement
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-pdostatement.rowcount-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>PDOStatement::rowCount</strong></span>(): <span class="type"><a href="language.types.integer.php" class="type int">int</a></span></div>


  <p class="para rdfs-comment">
   <span class="methodname"><strong>PDOStatement::rowCount()</strong></span> returns the number of
   rows affected by the last DELETE, INSERT, or UPDATE statement
   executed by the corresponding <code class="literal">PDOStatement</code> object. 
  </p>
  <p class="para">
   For statements that produce result sets, such as <code class="literal">SELECT</code>,
   the behavior is undefined and can be different for each driver.
   Some databases may return the number of rows produced by that statement
   (e.g. MySQL in buffered mode), but this
   behaviour is not guaranteed for all databases and should not be relied
   on for portable applications.
  </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      This method returns &quot;0&quot; (zero) with PostgreSQL driver when setting the
      <strong><code><a href="pdo.constants.php#pdo.constants.attr-cursor">PDO::ATTR_CURSOR</a></code></strong> statement attribute to
      <strong><code><a href="pdo.constants.php#pdo.constants.cursor-scroll">PDO::CURSOR_SCROLL</a></code></strong>.
     </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-pdostatement.rowcount-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">This function has no parameters.</p>
 </div>

 
  <div class="refsect1 returnvalues" id="refsect1-pdostatement.rowcount-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns the number of rows.
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-pdostatement.rowcount-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
Emits an error with level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> is set
to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>.
</p>
<p class="para">
Throws a <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong>
is set to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>.
</p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-pdostatement.rowcount-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 Return the number of deleted rows</strong></p>
    <div class="example-contents"><p>
     <span class="methodname"><strong>PDOStatement::rowCount()</strong></span> returns the number of
     rows affected by a DELETE, INSERT, or UPDATE statement.
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/* Delete all rows from the FRUIT table */<br /></span><span style="color: #0000BB">$del </span><span style="color: #007700">= </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">'DELETE FROM fruit'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$del</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">/* Return number of rows that were deleted */<br /></span><span style="color: #007700">print </span><span style="color: #DD0000">"Return number of rows that were deleted:\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">$del</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">rowCount</span><span style="color: #007700">();<br />print </span><span style="color: #DD0000">"Deleted </span><span style="color: #0000BB">$count</span><span style="color: #DD0000"> rows.\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">Return number of rows that were deleted:
Deleted 9 rows.</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 Counting rows returned by a SELECT statement</strong></p>
    <div class="example-contents"><p>
     For most databases, <span class="methodname"><strong>PDOStatement::rowCount()</strong></span> does not
     return the number of rows affected by a SELECT statement. Instead, use
     <span class="methodname"><a href="pdo.query.php" class="methodname">PDO::query()</a></span> to issue a SELECT COUNT(*) statement
     with the same predicates as your intended SELECT statement, then use
     <span class="methodname"><a href="pdostatement.fetchcolumn.php" class="methodname">PDOStatement::fetchColumn()</a></span> to retrieve the number
     of matching rows. 
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT COUNT(*) FROM fruit WHERE calories &gt; 100"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">$res</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">();<br /><br />print </span><span style="color: #DD0000">"There are " </span><span style="color: #007700">.  </span><span style="color: #0000BB">$count </span><span style="color: #007700">. </span><span style="color: #DD0000">" matching records."</span><span style="color: #007700">;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output
something similar to:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">There are 2 matching records.</pre>
</div>
    </div>
   </div>

  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-pdostatement.rowcount-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="methodname"><a href="pdostatement.columncount.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::columnCount()</a> - Returns the number of columns in the result set</span></li>
    <li><span class="methodname"><a href="pdostatement.fetchcolumn.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</span></li>
    <li><span class="methodname"><a href="pdo.query.php" class="methodname" rel="rdfs-seeAlso">PDO::query()</a> - Prepares and executes an SQL statement without placeholders</span></li>
   </ul>
  </p>

 </div>

</div><?php manual_footer($setup); ?>