2007-12-26 14:41:04 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
2009-07-11 08:31:40 +00:00
|
|
|
<!-- $Revision$ -->
|
2007-12-26 14:41:04 +00:00
|
|
|
|
|
|
|
<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
|
2007-12-31 15:43:38 +00:00
|
|
|
to inspect using the <function>PDO::errorCode</function> and
|
|
|
|
<function>PDO::errorInfo</function> methods on both the
|
2007-12-26 14:41:04 +00:00
|
|
|
statement and database objects; if the error resulted from a call on a
|
|
|
|
statement object, you would invoke the
|
2007-12-31 15:43:38 +00:00
|
|
|
<function>PDOStatement::errorCode</function> or
|
|
|
|
<function>PDOStatement::errorInfo</function>
|
2007-12-26 14:41:04 +00:00
|
|
|
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
|
2007-12-31 15:43:38 +00:00
|
|
|
appropriate SQLSTATE codes. The <function>PDO::errorCode</function>
|
2007-12-26 14:41:04 +00:00
|
|
|
method returns a single SQLSTATE code. If you need more specific
|
|
|
|
information about an error, PDO also offers an
|
2007-12-31 15:43:38 +00:00
|
|
|
<function>PDO::errorInfo</function> method which returns an array
|
2007-12-26 14:41:04 +00:00
|
|
|
containing the SQLSTATE code, the driver specific error code and driver
|
|
|
|
specific error string.
|
|
|
|
</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
|
2009-09-25 07:04:39 +00:00
|
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
2007-12-26 14:41:04 +00:00
|
|
|
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
|
|
|
|
-->
|
|
|
|
|