<!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>Errors and error handling</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.prepared-statements.html">Prepared statements and stored procedures</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.lobs.html">Large Objects (LOBs)</a></div> <div class="up"><a href="book.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="pdo.error-handling" class="chapter"> <h1>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>PDO::ERRMODE_SILENT</code></strong> </p> <p class="para"> This is the default mode. PDO will simply set the error code for you to inspect using the <span class="function"><a href="pdo.errorcode.html" class="function">PDO::errorCode()</a></span> and <span class="function"><a href="pdo.errorinfo.html" class="function">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="function"><a href="pdostatement.errorcode.html" class="function">PDOStatement::errorCode()</a></span> or <span class="function"><a href="pdostatement.errorinfo.html" class="function">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>PDO::ERRMODE_WARNING</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>PDO::ERRMODE_EXCEPTION</code></strong> </p> <p class="para"> In addition to setting the error code, PDO will throw a <a href="class.pdoexception.html" class="classname">PDOException</a> and set its properties to reflect the error code and error information. This setting is also useful during debugging, as it will effectively "blow up" 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.html" 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="function"><a href="pdo.errorcode.html" class="function">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="function"><a href="pdo.errorinfo.html" class="function">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-910"> <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"><?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 />try {<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">-></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 />} catch (</span><span style="color: #0000BB">PDOException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Connection failed: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br />}<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> <span class="function"><a href="pdo.construct.html" class="function">PDO::__construct()</a></span> will always throw a <a href="class.pdoexception.html" class="classname">PDOException</a> if the connection fails regardless of which <strong><code>PDO::ATTR_ERRMODE</code></strong> is currently set. Uncaught Exceptions are fatal. </p> </p></blockquote> <p class="para"> <div class="example" id="example-911"> <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"><?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: #FF8000">/*<br /> Using try/catch around the constructor is still valid even though we set the ERRMODE to WARNING since<br /> PDO::__construct will always throw a PDOException if the connection fails.<br />*/<br /></span><span style="color: #007700">try {<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">=> </span><span style="color: #0000BB">PDO</span><span style="color: #007700">::</span><span style="color: #0000BB">ERRMODE_WARNING</span><span style="color: #007700">));<br />} catch (</span><span style="color: #0000BB">PDOException $e</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">'Connection failed: ' </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">();<br /> exit;<br />}<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">-></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">);<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> Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.wrongtable' doesn't exist in /tmp/pdo_test.php on line 18 </pre></div> </div> </div> </p> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="pdo.prepared-statements.html">Prepared statements and stored procedures</a></div> <div class="next" style="text-align: right; float: right;"><a href="pdo.lobs.html">Large Objects (LOBs)</a></div> <div class="up"><a href="book.pdo.html">PDO</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>