<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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',
    2 => 'Execute an SQL statement and return the number of affected rows',
  ),
  'up' => 
  array (
    0 => 'class.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.errorinfo.php',
    1 => 'PDO::errorInfo',
  ),
  'next' => 
  array (
    0 => 'pdo.getattribute.php',
    1 => 'PDO::getAttribute',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo/pdo/exec.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.exec" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">PDO::exec</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8, PECL pdo &gt;= 0.1.0)</p><p class="refpurpose"><span class="refname">PDO::exec</span> &mdash; <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="methodname"><strong>PDO::exec</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.string.php" class="type string">string</a></span> <code class="parameter">$statement</code></span>): <span class="type"><span class="type"><a href="language.types.integer.php" class="type int">int</a></span>|<span class="type"><a href="language.types.singleton.php" class="type false">false</a></span></span></div>


  <p class="para rdfs-comment">
   <span class="methodname"><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="methodname"><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="methodname"><a href="pdo.query.php" class="methodname">PDO::query()</a></span>.
   For a statement that you need to issue multiple times, prepare 
   a PDOStatement object with <span class="methodname"><a href="pdo.prepare.php" class="methodname">PDO::prepare()</a></span> and issue
   the statement with <span class="methodname"><a href="pdostatement.execute.php" class="methodname">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><code class="parameter">statement</code></dt>
      <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.php" class="link">properly escaped</a>.
       </p>
      </dd>
     
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-pdo.exec-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   <span class="methodname"><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="methodname"><strong>PDO::exec()</strong></span> returns <code class="literal">0</code>. 
  </p>

  <div class="warning"><strong class="warning">Warning</strong><p class="simpara">This function may
return Boolean <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>, but may also return a non-Boolean value which
evaluates to <strong><code><a href="reserved.constants.php#constant.false">false</a></code></strong>. 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
   <span class="methodname"><strong>PDO::exec()</strong></span>, wherein a statement that affected 0 rows
   results in a call to <span class="function"><a href="function.die.php" class="function">die()</a></span>:
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$db</span><span style="color: #007700">-&gt;</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">-&gt;</span><span style="color: #0000BB">errorInfo</span><span style="color: #007700">(), </span><span style="color: #0000BB">true</span><span style="color: #007700">)); </span><span style="color: #FF8000">// incorrect<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </p>

 </div>


 <div class="refsect1 errors" id="refsect1-pdo.exec-errors">
  <h3 class="title">Errors/Exceptions</h3>
  <p class="para">
Emits an error with level <strong><code><a href="errorfunc.constants.php#constant.e-warning">E_WARNING</a></code></strong> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> is set
to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>.
</p>
<p class="para">
Throws a <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> if the attribute <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong>
is set to <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>.
</p>
 </div>


 <div class="refsect1 examples" id="refsect1-pdo.exec-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <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">&lt;?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">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"DELETE FROM fruit"</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">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="examplescode"><pre class="examplescode">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><span class="methodname"><a href="pdo.prepare.php" class="methodname" rel="rdfs-seeAlso">PDO::prepare()</a> - Prepares a statement for execution and returns a statement object</span></li>
    <li><span class="methodname"><a href="pdo.query.php" class="methodname" rel="rdfs-seeAlso">PDO::query()</a> - Prepares and executes an SQL statement without placeholders</span></li>
    <li><span class="methodname"><a href="pdostatement.execute.php" class="methodname" rel="rdfs-seeAlso">PDOStatement::execute()</a> - Executes a prepared statement</span></li>
   </ul>
  </p>
 </div>



</div><?php manual_footer($setup); ?>