Source of: /manual/en/pdostatement.rowcount.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.rowcount.php',
1 => 'PDOStatement->rowCount',
),
'up' =>
array (
0 => 'class.pdostatement.php',
1 => 'The PDOStatement class',
),
'prev' =>
array (
0 => 'pdostatement.nextrowset.php',
1 => 'PDOStatement->nextRowset',
),
'next' =>
array (
0 => 'pdostatement.setattribute.php',
1 => 'PDOStatement->setAttribute',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="pdostatement.rowcount" class="refentry">
<div class="refnamediv">
<h1 class="refname">PDOStatement->rowCount</h1>
<p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo >= 0.1.0)</p><p class="refpurpose"><span class="refname">PDOStatement->rowCount</span> — <span class="dc-title">
Returns the number of rows affected by the last SQL statement
</span></p>
</div>
<a name="pdostatement.rowcount.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::rowCount</b></span>
( <span class="methodparam">void</span>
)</div>
<p class="para rdfs-comment">
<b>PDOStatement::rowCount()</b> returns the number of
rows affected by the last DELETE, INSERT, or UPDATE statement
executed by the corresponding <i>PDOStatement</i> object.
</p>
<p class="para">
If the last SQL statement executed by the associated
<i>PDOStatement</i> was a SELECT statement, some databases
may return the number of rows returned by that statement. However, this
behaviour is not guaranteed for all databases and should not be relied
on for portable applications.
</p>
</div>
<a name="pdostatement.rowcount.returnvalues"></a><div class="refsect1 returnvalues">
<h3 class="title">Return Values</h3>
<p class="para">
Returns the number of rows.
</p>
</div>
<a name="pdostatement.rowcount.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Return the number of deleted rows</b></p>
<div class="example-contents para"><p>
<b>PDOStatement::rowCount()</b> returns the number of
rows affected by a DELETE, INSERT, or UPDATE statement.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?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">-></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">-></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">-></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">?></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>
Deleted 9 rows.
</pre></div>
</div>
</div><p>
</p><div class="example">
<p><b>Example #2 Counting rows returned by a SELECT statement</b></p>
<div class="example-contents para"><p>
For most databases, <b>PDOStatement::rowCount()</b> does not
return the number of rows affected by a SELECT statement. Instead, use
<a href="pdo.query.php" class="function">PDO::query()</a> to issue a SELECT COUNT(*) statement
with the same predicates as your intended SELECT statement, then use
<a href="pdostatement.fetchcolumn.php" class="function">PDOStatement::fetchColumn()</a> to retrieve the number
of rows that will be returned. Your application can then perform the
correct action.
</p></div>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT COUNT(*) FROM fruit WHERE calories > 100"</span><span style="color: #007700">;<br />if (</span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">)) {<br /><br /> </span><span style="color: #FF8000">/* Check the number of rows that match the SELECT statement */<br /> </span><span style="color: #007700">if (</span><span style="color: #0000BB">$res</span><span style="color: #007700">-></span><span style="color: #0000BB">fetchColumn</span><span style="color: #007700">() > </span><span style="color: #0000BB">0</span><span style="color: #007700">) {<br /><br /> </span><span style="color: #FF8000">/* Issue the real SELECT statement and work with the results */<br /> </span><span style="color: #0000BB">$sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"SELECT name FROM fruit WHERE calories > 100"</span><span style="color: #007700">;<br /> foreach (</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-></span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #0000BB">$sql</span><span style="color: #007700">) as </span><span style="color: #0000BB">$row</span><span style="color: #007700">) {<br /> print </span><span style="color: #DD0000">"Name: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #DD0000">'NAME'</span><span style="color: #007700">] . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /> }<br /> }<br /> </span><span style="color: #FF8000">/* No rows matched -- do something else */<br /> </span><span style="color: #007700">else {<br /> print </span><span style="color: #DD0000">"No rows matched the query."</span><span style="color: #007700">;<br /> }<br />}<br /><br /></span><span style="color: #0000BB">$res </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$conn </span><span style="color: #007700">= </span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></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>
apple
banana
orange
pear
</pre></div>
</div>
</div><p>
</p>
</div>
<a name="pdostatement.rowcount.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="pdostatement.columncount.php" class="function" rel="rdfs-seeAlso">PDOStatement::columnCount()</a> - Returns the number of columns in the result set</li>
<li class="member"><a href="pdostatement.fetchcolumn.php" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</li>
<li class="member"><a href="pdo.query.php" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement object</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>