<!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>Execute an SQL statement and return the number of affected rows</title> </head> <body><div class="manualnavbar" 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.0)</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" id="refsect1-pdo.exec-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>PDO::exec</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$statement</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>PDO::exec()</strong></span> executes an SQL statement in a single function call, returning the number of rows affected by the statement. </p> <p class="para"> <span class="function"><strong>PDO::exec()</strong></span> does not return results from a SELECT statement. For a SELECT statement that you only need to issue once during your program, consider issuing <span class="function"><a href="pdo.query.html" class="function">PDO::query()</a></span>. For a statement that you need to issue multiple times, prepare a PDOStatement object with <span class="function"><a href="pdo.prepare.html" class="function">PDO::prepare()</a></span> and issue the statement with <span class="function"><a href="pdostatement.execute.html" class="function">PDOStatement::execute()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-pdo.exec-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">statement</code></em></span> <dd> <p class="para"> The SQL statement to prepare and execute. </p> <p class="para"> Data inside the query should be <a href="pdo.quote.html" class="link">properly escaped</a>. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-pdo.exec-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>PDO::exec()</strong></span> returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, <span class="function"><strong>PDO::exec()</strong></span> returns <em>0</em>. </p> <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may return Boolean <strong><code>FALSE</code></strong>, but may also return a non-Boolean value which evaluates to <strong><code>FALSE</code></strong>. Please read the section on <a href="language.types.boolean.html" class="link">Booleans</a> for more information. Use <a href="language.operators.comparison.html" class="link">the === operator</a> for testing the return value of this function.</p></div> <p class="para"> The following example incorrectly relies on the return value of <span class="function"><strong>PDO::exec()</strong></span>, wherein a statement that affected 0 rows results in a call to <span class="function"><a href="function.die.html" class="function">die()</a></span>: <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">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-></span><span style="color: #0000BB">errorInfo</span><span style="color: #007700">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </p> </div> <div class="refsect1 examples" id="refsect1-pdo.exec-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-923"> <p><strong>Example #1 Issuing a DELETE statement</strong></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 </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> Deleted 1 rows. </pre></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-pdo.exec-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="pdo.prepare.html" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</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> <li class="member"> <span class="function"><a href="pdostatement.execute.html" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</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="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>