reference/pdo/error-handling.xml
1508d46d0998c4843a22d28460bb6c4244290129
...
...
@@ -13,13 +13,13 @@
13
13
<constant>PDO::ERRMODE_SILENT</constant>
14
14
</para>
15
15
<para>
16
-
This is the default mode. PDO will simply set the error code for you
17
-
to inspect using the <function>PDO::errorCode</function> and
18
-
<function>PDO::errorInfo</function> methods on both the
16
+
Prior to PHP 8.0.0, this was the default mode. PDO will simply set the error code for you
17
+
to inspect using the <methodname>PDO::errorCode</methodname> and
18
+
<methodname>PDO::errorInfo</methodname> methods on both the
19
19
statement and database objects; if the error resulted from a call on a
20
20
statement object, you would invoke the
21
-
<function>PDOStatement::errorCode</function> or
22
-
<function>PDOStatement::errorInfo</function>
21
+
<methodname>PDOStatement::errorCode</methodname> or
22
+
<methodname>PDOStatement::errorInfo</methodname>
23
23
method on that object. If the error resulted from a call on the
24
24
database object, you would invoke those methods on the database object
25
25
instead.
...
...
@@ -41,6 +41,7 @@
41
41
<constant>PDO::ERRMODE_EXCEPTION</constant>
42
42
</para>
43
43
<para>
44
+
As of PHP 8.0.0, this is the default mode.
44
45
In addition to setting the error code, PDO will throw a
45
46
<classname>PDOException</classname>
46
47
and set its properties to reflect the error code and error
...
...
@@ -65,10 +66,10 @@
65
66
<para>
66
67
PDO standardizes on using SQL-92 SQLSTATE error code strings; individual
67
68
PDO drivers are responsible for mapping their native codes to the
68
-
appropriate SQLSTATE codes. The <function>PDO::errorCode</function>
69
+
appropriate SQLSTATE codes. The <methodname>PDO::errorCode</methodname>
69
70
method returns a single SQLSTATE code. If you need more specific
70
71
information about an error, PDO also offers an
71
-
<function>PDO::errorInfo</function> method which returns an array
72
+
<methodname>PDO::errorInfo</methodname> method which returns an array
72
73
containing the SQLSTATE code, the driver specific error code and driver
73
74
specific error string.
74
75
</para>
...
...
@@ -83,22 +84,29 @@ $dsn = 'mysql:dbname=testdb;host=127.0.0.1';
83
84
$user = 'dbuser';
84
85
$password = 'dbpass';
85
86

86
-
try {
87
-
$dbh = new PDO($dsn, $user, $password);
88
-
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
89
-
} catch (PDOException $e) {
90
-
echo 'Connection failed: ' . $e->getMessage();
91
-
}
87
+
$dbh = new PDO($dsn, $user, $password);
88
+
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
92
89

93
-
?>
90
+
// This will cause PDO to throw a PDOException (when the table doesn't exist)
91
+
$dbh->query("SELECT wrongcolumn FROM wrongtable");
94
92
]]>
95
93
</programlisting>
94
+
&example.outputs;
95
+
<screen>
96
+
<![CDATA[
97
+
Fatal error: Uncaught PDOException: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'testdb.wrongtable' doesn't exist in /tmp/pdo_test.php:10
98
+
Stack trace:
99
+
#0 /tmp/pdo_test.php(10): PDO->query('SELECT wrongcol...')
100
+
#1 {main}
101
+
thrown in /tmp/pdo_test.php on line 10
102
+
]]>
103
+
</screen>
96
104
</example>
97
105
</para>
98
106
<note>
99
107
<para>
100
-
<function>PDO::__construct</function> will always throw a <classname>PDOException</classname> if the connection fails
101
-
regardless of which <constant>PDO::ATTR_ERRMODE</constant> is currently set. Uncaught Exceptions are fatal.
108
+
<methodname>PDO::__construct</methodname> will always throw a <classname>PDOException</classname> if the connection fails
109
+
regardless of which <constant>PDO::ATTR_ERRMODE</constant> is currently set.
102
110
</para>
103
111
</note>
104
112
<para>
...
...
@@ -111,27 +119,17 @@ $dsn = 'mysql:dbname=test;host=127.0.0.1';
111
119
$user = 'googleguy';
112
120
$password = 'googleguy';
113
121

114
-
/*
115
-
Using try/catch around the constructor is still valid even though we set the ERRMODE to WARNING since
116
-
PDO::__construct will always throw a PDOException if the connection fails.
117
-
*/
118
-
try {
119
-
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
120
-
} catch (PDOException $e) {
121
-
echo 'Connection failed: ' . $e->getMessage();
122
-
exit;
123
-
}
122
+
$dbh = new PDO($dsn, $user, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING));
124
123

125
124
// This will cause PDO to throw an error of level E_WARNING instead of an exception (when the table doesn't exist)
126
125
$dbh->query("SELECT wrongcolumn FROM wrongtable");
127
-
?>
128
126
]]>
129
127
</programlisting>
130
128
&example.outputs;
131
129
<screen>
132
130
<![CDATA[
133
131
Warning: PDO::query(): SQLSTATE[42S02]: Base table or view not found: 1146 Table 'test.wrongtable' doesn't exist in
134
-
/tmp/pdo_test.php on line 18
132
+
/tmp/pdo_test.php on line 9
135
133
]]>
136
134
</screen>
137
135
</example>
138
136