php-doc-en/reference/pdo/error-handling.xml
Sherif Ramadan cfcfe6e059 Updated PDO error handling chapter with new example and added note to reflect PDO::__construct behavior.
This fixes Bug #63845 in that a note is added to inform the user that PDO::__construct will always throw an exception if the connection fails.
A new example is also added to reflect that even when the PDO::ATTR_ERRMODE is set in the constructor, and the connection fails, a PDOException will still be thrown.


git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@329077 c90b9560-bf6c-de11-be94-00142212c4b1
2013-01-11 11:09:57 +00:00

161 lines
5.1 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<chapter xml:id="pdo.error-handling" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Errors and error handling</title>
<para>
PDO offers you a choice of 3 different error handling strategies, to fit
your style of application development.
</para>
<itemizedlist>
<listitem>
<para>
<constant>PDO::ERRMODE_SILENT</constant>
</para>
<para>
This is the default mode. PDO will simply set the error code for you
to inspect using the <function>PDO::errorCode</function> and
<function>PDO::errorInfo</function> methods on both the
statement and database objects; if the error resulted from a call on a
statement object, you would invoke the
<function>PDOStatement::errorCode</function> or
<function>PDOStatement::errorInfo</function>
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.
</para>
</listitem>
<listitem>
<para>
<constant>PDO::ERRMODE_WARNING</constant>
</para>
<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.
</para>
</listitem>
<listitem>
<para>
<constant>PDO::ERRMODE_EXCEPTION</constant>
</para>
<para>
In addition to setting the error code, PDO will throw a
<classname>PDOException</classname>
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).
</para>
<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.
</para>
<para>
See <link linkend="language.exceptions">Exceptions</link> for more
information about Exceptions in PHP.
</para>
</listitem>
</itemizedlist>
<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 <function>PDO::errorCode</function>
method returns a single SQLSTATE code. If you need more specific
information about an error, PDO also offers an
<function>PDO::errorInfo</function> method which returns an array
containing the SQLSTATE code, the driver specific error code and driver
specific error string.
</para>
<para>
<example>
<title>Create a PDO instance and set the error mode</title>
<programlisting role="php">
<![CDATA[
<?php
$dsn = 'mysql:dbname=testdb;host=127.0.0.1';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
]]>
</programlisting>
</example>
</para>
<note>
<para>
<function>PDO::__construct</function> will always throw a <classname>PDOException</classname> if the connection fails
regardless of which <constant>PDO::ATTR_ERRMODE</constant> is currently set. Uncaught Exceptions are fatal.
</para>
</note>
<para>
<example>
<title>Create a PDO instance and set the error mode from the constructor</title>
<programlisting role="php">
<![CDATA[
<?php
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'googleguy';
$password = 'googleguy';
/*
Using try/catch around the constructor is still valid even though we set the ERRMODE to WARNING since
PDO::__construct will always throw a PDOException if the connection fails.
*/
try {
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
exit;
}
// This will cause PDO to throw an error of level E_WARNING instead of an exception (when the table doesn't exist)
$dbh->query("SELECT wrongcolumn FROM wrongtable");
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
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
]]>
</screen>
</example>
</para>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->