Skip to content

Commit 5075240

Browse files
AllenJBcmb69
authored andcommitted
Change the default PDO error mode to exceptions
According to <https://www.php.net/manual/en/pdo.error-handling.php>.
1 parent 8ffbd46 commit 5075240

15 files changed

+72
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ PHP NEWS
9393
. Don't ignore invalid escape sequences. (sjon)
9494

9595
- PDO:
96+
. Changed default PDO error mode to exceptions. (AllenJB)
9697
. Fixed bug #77849 (Disable cloning of PDO handle/connection objects).
9798
(camporter)
9899

UPGRADING

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ PHP 8.0 UPGRADE NOTES
291291
now ignored.
292292

293293
- PDO:
294+
. The default error handling mode has been changed from "silent" to
295+
"exceptions". See https://www.php.net/manual/en/pdo.error-handling.php
296+
for details of behavior changes and how to explicitly set this attribute.
297+
RFC: https://wiki.php.net/rfc/pdo_default_errmode
294298
. The method PDOStatement::setFetchMode() now accepts the following signature:
295299

296300
PDOStatement::setFetchMode($mode, $classname, $params)

ext/pdo/pdo_dbh.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ PHP_METHOD(PDO, __construct)
340340
}
341341

342342
dbh->auto_commit = pdo_attr_lval(options, PDO_ATTR_AUTOCOMMIT, 1);
343+
dbh->error_mode = pdo_attr_lval(options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
343344

344345
if (!dbh->data_source || (username && !dbh->username) || (password && !dbh->password)) {
345346
php_error_docref(NULL, E_ERROR, "Out of memory");

ext/pdo/tests/bug_44159.phpt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ try {
1212
--FILE--
1313
<?php
1414
$pdo = new PDO("sqlite:".__DIR__."/foo.db");
15+
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
1516

1617
$attrs = array(PDO::ATTR_STATEMENT_CLASS, PDO::ATTR_STRINGIFY_FETCHES, PDO::NULL_TO_STRING);
1718

@@ -26,15 +27,23 @@ foreach ($attrs as $attr) {
2627
?>
2728
--EXPECTF--
2829
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); the classname must be a string specifying an existing class in %s on line %d
30+
31+
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error in %s on line %d
2932
bool(false)
3033

3134
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); the classname must be a string specifying an existing class in %s on line %d
35+
36+
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error in %s on line %d
3237
bool(false)
3338

3439
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: PDO::ATTR_STATEMENT_CLASS requires format array(classname, array(ctor_args)); the classname must be a string specifying an existing class in %s on line %d
40+
41+
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error in %s on line %d
3542
bool(false)
3643

3744
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error: attribute value must be an integer in %s on line %d
45+
46+
Warning: PDO::setAttribute(): SQLSTATE[HY000]: General error in %s on line %d
3847
bool(false)
3948
bool(true)
4049
bool(true)

ext/pdo/tests/pdo_test.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class PDOTest {
3737
if (!$db) {
3838
die("Could not create PDO object (DSN=$dsn, user=$user)\n");
3939
}
40+
// Ignore errors about non-existant tables
41+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
4042

4143
// clean up any crufty test tables we might have left behind
4244
// on a previous run

ext/pdo_mysql/tests/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ PDO_MYSQL_TEST_CHARSET
3131
NOTE: if any of `PDO_MYSQL_TEST_[HOST|DB|SOCKET|ENGINE|CHARSET]` is part of
3232
`PDO_MYSQL_TEST_DSN`, the values must match. That is, for example, for
3333
`PDO_MYSQL_TEST_DSN = mysql:dbname=test` you MUST set `PDO_MYSQL_TEST_DB=test`.
34+
35+
## MySQL User Permissions
36+
37+
The MySQL user used to run the tests must have full permissions on the test
38+
database, plus the following additional permissions:
39+
40+
* SUPER: Required to [create functions if binary logging is enabled](https://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html#sa38412929)
41+
* SELECT permissions on performance_schema.session_connect_attrs

ext/pdo_mysql/tests/pdo_mysql___construct.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,8 @@ MySQLPDOTest::skip();
150150
$dsn = MySQLPDOTest::getDSN(array('dbname' => $db), 'dbname=' . $invalid_db);
151151
try { $db = @new PDO($dsn, $user, $pass); assert(false); printf("%s\n", $dsn); } catch (PDOException $e) {
152152
$tmp = $e->getMessage();
153-
if (!stristr($tmp, '42000') && !stristr($tmp, '1049'))
153+
// 1044 may occur here if running tests using a custom user that does not have access to all databases
154+
if (!stristr($tmp, '42000') && !stristr($tmp, '1049') && !stristr($tmp, '1044'))
154155
printf("[022] Cannot find proper error codes: %s\n", $tmp);
155156
}
156157

ext/pdo_mysql/tests/pdo_mysql___construct_options.phpt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ MySQLPDOTest::skip();
1818

1919
try {
2020
$db = new PDO($dsn, $user, $pass, array($option => $value));
21+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
2122
if (!is_object($db) || ($value !== ($tmp = @$db->getAttribute($option))))
2223
printf("[%03d] Expecting '%s'/%s got '%s'/%s' for options '%s'\n",
2324
$offset,
@@ -81,6 +82,7 @@ MySQLPDOTest::skip();
8182
printf("[003] [TODO][CHANGEREQUEST] Please, lets not ignore invalid options and bail out!\n");
8283

8384
$db = new PDO($dsn, $user, $pass);
85+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
8486
foreach ($valid_options as $option => $name) {
8587
/* TODO getAttribute() is pretty poor in supporting the options, suppress errors */
8688
$tmp = @$db->getAttribute($option);
@@ -155,10 +157,11 @@ MySQLPDOTest::skip();
155157
set_option_and_check(34, PDO::MYSQL_ATTR_DIRECT_QUERY, 0, 'PDO::MYSQL_ATTR_DIRECT_QUERY');
156158

157159
} catch (PDOException $e) {
158-
printf("[001] %s, [%s] %s\n",
160+
printf("[001] %s, [%s] %s Line: %s\n",
159161
$e->getMessage(),
160162
(is_object($db)) ? $db->errorCode() : 'n/a',
161-
(is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a');
163+
(is_object($db)) ? implode(' ', $db->errorInfo()) : 'n/a',
164+
$e->getLine());
162165
}
163166

164167
print "done!";

ext/pdo_mysql/tests/pdo_mysql_attr_multi_statements.phpt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ error_reporting=E_ALL
1919

2020
$table = sprintf("test_%s", md5(mt_rand(0, PHP_INT_MAX)));
2121
$db = new PDO($dsn, $user, $pass);
22+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
2223
$db->exec(sprintf('DROP TABLE IF EXISTS %s', $table));
2324
$create = sprintf('CREATE TABLE %s(id INT)', $table);
2425
$db->exec($create);
@@ -35,6 +36,7 @@ error_reporting=E_ALL
3536

3637
// New connection, does not allow multiple statements.
3738
$db = new PDO($dsn, $user, $pass, array(PDO::MYSQL_ATTR_MULTI_STATEMENTS => false));
39+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
3840
$stmt = $db->query(sprintf('SELECT * FROM %s; INSERT INTO %s(id) VALUES (3)', $table, $table));
3941
var_dump($stmt);
4042
$info = $db->errorInfo();
@@ -49,7 +51,7 @@ error_reporting=E_ALL
4951
$db->exec(sprintf('DROP TABLE IF EXISTS %s', $table));
5052
print "done!";
5153
?>
52-
--EXPECT--
54+
--EXPECTF--
5355
string(5) "00000"
5456
array(2) {
5557
[0]=>
@@ -70,6 +72,8 @@ array(1) {
7072
string(1) "1"
7173
}
7274
}
75+
76+
Warning: PDO::query(): SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO %s(id) VALUES (3)' at line 1 in %s on line %d
7377
bool(false)
7478
string(5) "42000"
7579
array(2) {

ext/pdo_mysql/tests/pdo_mysql_exec.phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ MySQLPDOTest::skip();
176176
<?php
177177
require __DIR__ . '/mysql_pdo_test.inc';
178178
$db = MySQLPDOTest::factory();
179+
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
179180
@$db->exec('DROP TABLE IF EXISTS test');
180181
?>
181182
--EXPECTF--

0 commit comments

Comments
 (0)