pdostatement.rowcount.html
来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 124 行
HTML
124 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Returns the number of rows affected by the last SQL statement</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div 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">(No version information available, might be only in CVS)</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"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">int</span> <span class="methodname"><b><b>PDOStatement::rowCount</b></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> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the number of rows. </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Return the number of deleted rows</b></p> <div class="example-contents"><p> <b>PDOStatement::rowCount()</b> 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 $count 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"><pre><div class="cdata"><pre>Deleted 9 rows.</pre></div> </pre></div> </div> <div class="example"> <p><b>Example #2 Counting rows returned by a SELECT statement</b></p> <div class="example-contents"><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.html" 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.html" 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"><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"><pre><div class="cdata"><pre>applebananaorangepear</pre></div> </pre></div> </div> </p> </div> <div class="refsect1 seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><a href="pdostatement.columncount.html" class="function" rel="rdfs-seeAlso">PDOStatement::columnCount()</a></li> <li class="member"><a href="pdostatement.fetchcolumn.html" class="function" rel="rdfs-seeAlso">PDOStatement::fetchColumn()</a></li> <li class="member"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a></li> </ul> </p> </div></div><hr /><div 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>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?