<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/book.pdo.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'pdo.error-handling.php',
    1 => 'Errors and error handling',
    2 => 'Errors and error handling',
  ),
  'up' => 
  array (
    0 => 'book.pdo.php',
    1 => 'PDO',
  ),
  'prev' => 
  array (
    0 => 'pdo.prepared-statements.php',
    1 => 'Prepared statements and stored procedures',
  ),
  'next' => 
  array (
    0 => 'pdo.lobs.php',
    1 => 'Large Objects (LOBs)',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/pdo/error-handling.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="pdo.error-handling" class="chapter">
 <h1 class="title">Errors and error handling</h1>

 <p class="para">
  PDO offers you a choice of 3 different error handling strategies, to fit
  your style of application development.
 </p>
 <ul class="itemizedlist">
  <li class="listitem">
   <p class="para">
    <strong><code><a href="pdo.constants.php#pdo.constants.errmode-silent">PDO::ERRMODE_SILENT</a></code></strong>
   </p>
   <p class="para">
     Prior to PHP 8.0.0, this was the default mode. PDO will simply set the error code for you
     to inspect using the <span class="methodname"><a href="pdo.errorcode.php" class="methodname">PDO::errorCode()</a></span> and
     <span class="methodname"><a href="pdo.errorinfo.php" class="methodname">PDO::errorInfo()</a></span> methods on both the
     statement and database objects; if the error resulted from a call on a
     statement object, you would invoke the
     <span class="methodname"><a href="pdostatement.errorcode.php" class="methodname">PDOStatement::errorCode()</a></span> or
     <span class="methodname"><a href="pdostatement.errorinfo.php" class="methodname">PDOStatement::errorInfo()</a></span>
     method on that object. If the error resulted from a call on the
     database object, you would invoke those methods on the database object
     instead.
    </p>
  </li>
  <li class="listitem">
   <p class="para">
    <strong><code><a href="pdo.constants.php#pdo.constants.errmode-warning">PDO::ERRMODE_WARNING</a></code></strong>
   </p>
   <p class="para">
     In addition to setting the error code, PDO will emit a traditional
     E_WARNING message. This setting is useful during debugging/testing, if
     you just want to see what problems occurred without interrupting the
     flow of the application.
    </p>
   </li>
   <li class="listitem">
    <p class="para">
     <strong><code><a href="pdo.constants.php#pdo.constants.errmode-exception">PDO::ERRMODE_EXCEPTION</a></code></strong>
    </p>
    <p class="para">
     As of PHP 8.0.0, this is the default mode.
     In addition to setting the error code, PDO will throw a
     <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span>
     and set its properties to reflect the error code and error
     information. This setting is also useful during debugging, as it will
     effectively &quot;blow up&quot; the script at the point of the error, very
     quickly pointing a finger at potential problem areas in your code
     (remember: transactions are automatically rolled back if the exception
     causes the script to terminate).
    </p>
    <p class="para">
     Exception mode is also useful because you can structure your error
     handling more clearly than with traditional PHP-style warnings, and
     with less code/nesting than by running in silent mode and explicitly
     checking the return value of each database call.
    </p>
    <p class="para">
     See <a href="language.exceptions.php" class="link">Exceptions</a> for more
     information about Exceptions in PHP.
    </p>
   </li>
 </ul>
 <p class="para">
  PDO standardizes on using SQL-92 SQLSTATE error code strings; individual
  PDO drivers are responsible for mapping their native codes to the
  appropriate SQLSTATE codes.   The <span class="methodname"><a href="pdo.errorcode.php" class="methodname">PDO::errorCode()</a></span>
  method returns a single SQLSTATE code. If you need more specific
  information about an error, PDO also offers an
  <span class="methodname"><a href="pdo.errorinfo.php" class="methodname">PDO::errorInfo()</a></span> method which returns an array
  containing the SQLSTATE code, the driver specific error code and driver
  specific error string.
 </p>
 
 <p class="para">
  <div class="example" id="example-1">
   <p><strong>Example #1 Create a PDO instance and set the error mode</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$dsn </span><span style="color: #007700">= </span><span style="color: #DD0000">'mysql:dbname=testdb;host=127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user </span><span style="color: #007700">= </span><span style="color: #DD0000">'dbuser'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password </span><span style="color: #007700">= </span><span style="color: #DD0000">'dbpass'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setAttribute</span><span style="color: #007700">(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_ERRMODE</span><span style="color: #007700">, </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ERRMODE_EXCEPTION</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// This will cause PDO to throw a PDOException (when the table doesn't exist)<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT wrongcolumn FROM wrongtable"</span><span style="color: #007700">);</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>
Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table &#039;testdb.wrongtable&#039; doesn&#039;t exist in /tmp/pdo_test.php:10
Stack trace:
#0 /tmp/pdo_test.php(10): PDO-&gt;query(&#039;SELECT wrongcol...&#039;)
#1 {main}
  thrown in /tmp/pdo_test.php on line 10
</pre></div>
    </div>
  </div>
 </p>
 <blockquote class="note"><p><strong class="note">Note</strong>: 
  <p class="para">
   <span class="methodname"><a href="pdo.construct.php" class="methodname">PDO::__construct()</a></span> will always throw a <span class="classname"><a href="class.pdoexception.php" class="classname">PDOException</a></span> if the connection fails
   regardless of which <strong><code><a href="pdo.constants.php#pdo.constants.attr-errmode">PDO::ATTR_ERRMODE</a></code></strong> is currently set.
  </p>
 </p></blockquote>
 <p class="para">
  <div class="example" id="example-2">
   <p><strong>Example #2 Create a PDO instance and set the error mode from the constructor</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$dsn </span><span style="color: #007700">= </span><span style="color: #DD0000">'mysql:dbname=test;host=127.0.0.1'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$user </span><span style="color: #007700">= </span><span style="color: #DD0000">'googleguy'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$password </span><span style="color: #007700">= </span><span style="color: #DD0000">'googleguy'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$dbh </span><span style="color: #007700">= new </span><span style="color: #0000BB">PDO</span><span style="color: #007700">(</span><span style="color: #0000BB">$dsn</span><span style="color: #007700">, </span><span style="color: #0000BB">$user</span><span style="color: #007700">, </span><span style="color: #0000BB">$password</span><span style="color: #007700">, array(</span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ATTR_ERRMODE </span><span style="color: #007700">=&gt; </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ERRMODE_WARNING</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">// This will cause PDO to throw an error of level E_WARNING instead of an exception (when the table doesn't exist)<br /></span><span style="color: #0000BB">$dbh</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT wrongcolumn FROM wrongtable"</span><span style="color: #007700">);</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>
Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table &#039;test.wrongtable&#039; doesn&#039;t exist in
/tmp/pdo_test.php on line 9
</pre></div>
    </div>
  </div>
 </p>
</div>
<?php manual_footer($setup); ?>