Source of: /manual/en/pdo.exec.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/class.pdo.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'pdo.exec.php',
1 => 'PDO::exec',
),
'up' =>
array (
0 => 'class.pdo.php',
1 => 'The PDO class',
),
'prev' =>
array (
0 => 'pdo.errorinfo.php',
1 => 'PDO::errorInfo',
),
'next' =>
array (
0 => 'pdo.getattribute.php',
1 => 'PDO::getAttribute',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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>
<a name="pdo.exec.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>PDO::exec</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.php" class="function">PDO::query()</a>.
For a statement that you need to issue multiple times, prepare
a PDOStatement object with <a href="pdo.prepare.php" class="function">PDO::prepare()</a> and issue
the statement with <a href="pdostatement.execute.php" class="function">PDOStatement::execute()</a>.
</p>
</div>
<a name="pdo.exec.parameters"></a><div class="refsect1 parameters">
<h3 class="title">Parameters</h3>
<p class="para">
</p><dl>
<dt class="varlistentry">
<span class="term"><i><tt class="parameter">statement</tt></i>
</span>
</dt><dd class="listitem">
<p class="para">
The SQL statement to prepare and execute.
</p>
<p class="para">
Data inside the query should be <a href="pdo.quote.php" class="link">properly escaped</a>.
</p>
</dd>
</dl>
<p>
</p>
</div>
<a name="pdo.exec.returnvalues"></a><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 may
return Boolean <b><tt class="constant">FALSE</tt></b>, but may also return a non-Boolean value which
evaluates to <b><tt class="constant">FALSE</tt></b>, such as <i>0</i> or
"". Please read the section on <a href="language.types.boolean.php" class="link">Booleans</a> for more
information. Use <a href="language.operators.comparison.php" 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
<b>PDO::exec()</b>, wherein a statement that affected 0 rows
results in a call to <a href="function.die.php" class="function">die()</a>:
</p><div class="example-contents programlisting">
<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>
</p>
</div>
<a name="pdo.exec.examples"></a><div class="refsect1 examples">
<h3 class="title">Examples</h3>
<p class="para">
</p><div class="example">
<p><b>Example #1 Issuing a DELETE statement</b></p>
<div class="example-contents para"><p>
Count the number of rows deleted by a DELETE statement with no WHERE
clause.
</p></div>
<div class="example-contents programlisting">
<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 para"><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>
</p>
</div>
<a name="pdo.exec.seealso"></a><div class="refsect1 seealso">
<h3 class="title">See Also</h3>
<p class="para">
</p><ul class="simplelist">
<li class="member"><a href="pdo.prepare.php" class="function" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</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>
<li class="member"><a href="pdostatement.execute.php" class="function" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</li>
</ul><p>
</p>
</div>
</div><?php manual_footer(); ?>