<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Returns the number of rows affected by the last SQL statement</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.nextrowset.html">PDOStatement::nextRowset</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.setattribute.html">PDOStatement::setAttribute</a></div> <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><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> <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="type">int</span> <span class="methodname"><strong>PDOStatement::rowCount</strong></span> ( <span class="methodparam">void</span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>PDOStatement::rowCount()</strong></span> returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding <em>PDOStatement</em> object. </p> <p class="para"> If the last SQL statement executed by the associated <em>PDOStatement</em> 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> <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 examples" id="refsect1-pdostatement.rowcount-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-960"> <p><strong>Example #1 Return the number of deleted rows</strong></p> <div class="example-contents"><p> <span class="function"><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"><?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"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Return number of rows that were deleted: Deleted 9 rows. </pre></div> </div> </div> <div class="example" id="example-961"> <p><strong>Example #2 Counting rows returned by a SELECT statement</strong></p> <div class="example-contents"><p> For most databases, <span class="function"><strong>PDOStatement::rowCount()</strong></span> does not return the number of rows affected by a SELECT statement. Instead, use <span class="function"><a href="pdo.query.html" class="function">PDO::query()</a></span> to issue a SELECT COUNT(*) statement with the same predicates as your intended SELECT statement, then use <span class="function"><a href="pdostatement.fetchcolumn.html" class="function">PDOStatement::fetchColumn()</a></span> to retrieve the number of rows that will be returned. Your application can then perform the correct action. </p></div> <div class="example-contents"> <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"><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> </div> <div class="refsect1 seealso" id="refsect1-pdostatement.rowcount-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="pdostatement.columncount.html" class="function" rel="rdfs-seeAlso">PDOStatement::columnCount()</a> - Returns the number of columns in the result set</span></li> <li class="member"> <span class="function"><a href="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a> - Returns a single column from the next row of a result set</span></li> <li class="member"> <span class="function"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a> - Executes an SQL statement, returning a result set as a PDOStatement object</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdostatement.nextrowset.html">PDOStatement::nextRowset</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdostatement.setattribute.html">PDOStatement::setAttribute</a></div> <div class="up"><a href="class.pdostatement.html">PDOStatement</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>