📄 pdo.exec.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Execute an SQL statement and return the number of affected rows</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="pdo.errorinfo.html">PDO::errorInfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.getattribute.html">PDO::getAttribute</a></div> <div class="up"><a href="class.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="pdo.exec" class="refentry"> <div class="refnamediv"> <h1 class="refname">PDO::exec</h1> <p class="verinfo">(PHP 5 >= 5.1.0, PECL pdo:0.1-1.0.3)</p><p class="refpurpose"><span class="refname">PDO::exec</span> — <span class="dc-title"> Execute an SQL statement and return the number of affected rows </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>PDO::exec</b></b></span> ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$statement</tt></span> )</div> <p class="para rdfs-comment"> <b>PDO::exec()</b> executes an SQL statement in a single function call, returning the number of rows affected by the statement. </p> <p class="para"> <b>PDO::exec()</b> does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing <a href="pdo.query.html" class="function">PDO::query()</a>. For a statement that you need to issue multiple times, prepare a PDOStatement object with <a href="pdo.prepare.html" class="function">PDO::prepare()</a> and issue the statement with <a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a>. </p> </div> <div class="refsect1 parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><i><tt class="parameter">statement</tt></i></span> <dd> <p class="para"> The SQL statement to prepare and execute. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <b>PDO::exec()</b> returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, <b>PDO::exec()</b> returns <i>0</i>. </p> <div class="warning"><b class="warning">Warning</b><p class="simpara">This function mayreturn Boolean <b><tt>FALSE</tt></b>, but may also return a non-Boolean value whichevaluates to <b><tt>FALSE</tt></b>, such as <i>0</i> or"". Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for moreinformation. Use <a href="language.operators.comparison.html" class="link">the ===operator</a> for testing the return value of thisfunction.</p></div> <p class="para"> The following example incorrectly relies on the return value of <b>PDO::exec()</b>, wherein a statement that affected 0 rows results in a call to <a href="function.die.html" class="function">die()</a>: <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$db</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">() or die(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">errorInfo</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </p> </div> <div class="refsect1 examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example"> <p><b>Example #1 Issuing a DELETE statement</b></p> <div class="example-contents"><p> Count the number of rows deleted by a DELETE statement with no WHERE clause. </p></div> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #DD0000">'odbc:sample'</span><span style="color: #007700">, </span><span style="color: #DD0000">'db2inst1'</span><span style="color: #007700">, </span><span style="color: #DD0000">'ibmdb2'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Delete all rows from the FRUIT table */<br /></span><span style="color: #0000BB">$count </span><span style="color: #007700">= </span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-></span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"DELETE FROM fruit WHERE colour = 'red'"</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">"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 1 rows.</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="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a></li> <li class="member"><a href="pdo.query.html" class="function" rel="rdfs-seeAlso">PDO::query()</a></li> <li class="member"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a></li> </ul> </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.errorinfo.html">PDO::errorInfo</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.getattribute.html">PDO::getAttribute</a></div> <div class="up"><a href="class.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -