Bug #80267 [Opn->Csd]: PDO throws an exception when using fetchAll on non-select stmt with emulation
| From: | nikic@php.net | Date: | Tue, 08 Dec 2020 09:01:36 +0000 |
| Subject: | Bug #80267 [Opn->Csd]: PDO throws an exception when using fetchAll on non-select stmt with emulation | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-230925@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=80267&edit=1
ID: 80267
Updated by: nikic@php.net
Reported by: tekiela246 at gmail dot com
Summary: PDO throws an exception when using fetchAll on
non-select stmt with emulation
-Status: Open
+Status: Closed
Type: Bug
Package: PDO MySQL
Operating System: Windows 10
PHP Version: 7.3.23
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
This should be fixed by https://github.com/php/php-src/commit/a83cc03c138b8cf27a840bd7cd913eb7050e55ba.
Previous Comments:
------------------------------------------------------------------------
[2020-10-21 14:48:41] tekiela246 at gmail dot com
It happens when PDO::ATTR_EMULATE_PREPARES = true
------------------------------------------------------------------------
[2020-10-21 14:46:47] tekiela246 at gmail dot com
Description:
------------
When emulated prepares are switched off PDO returns an empty array for statements like UPDATE or
INSERT.
When emulated prepares are switched on PDO throws an exception with no description.
The problem comes from the following "bug fix" https://github.com/php/php-src/commit/2f3e330ad00522fc007a48132195e44dd47252ed
This fix introduced an exception but created an inconsistency with emulated prepares. If we assume
that the original bug report was indeed a bug then this means we need a better fix. However, I would
argue that fetchAll should not throw an exception if no result is produced.
For cases where no result is produced PDO should return false, but without union types such fix will
be a breaking change. e.g. https://github.com/paragonie/easydb/blob/master/src/EasyDB.php#L1310
Userland code expects PDO to return false only on error, which it doesn't do. (unless there is
a scenario of which I am not aware that fetchAll would return false?)
In conclusion, I would suggest reverting the previous fix https://github.com/php/php-src/commit/2f3e330ad00522fc007a48132195e44dd47252ed
Test script:
---------------
<?php
$pdo = new PDO("mysql:host=localhost;dbname=test;charset=utf8mb4", 'root',
'', [
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
]);
try {
$pdo->exec('CREATE TABLE pdo_test_emulated (
id int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB ;');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $pdo->prepare('INSERT INTO pdo_test_emulated(name) value("some
name")');
$stmt->execute();
var_dump($stmt->fetchAll());
// with emulated prepares an exception is generated
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
$stmt = $pdo->prepare('INSERT INTO pdo_test_emulated(name) value("some
name")');
$stmt->execute();
var_dump($stmt->fetchAll()); // exception triggered instead of an empty array
} finally {
$pdo->exec('DROP TABLE pdo_test_emulated');
}
Expected result:
----------------
array(0) {
}
array(0) {
}
Actual result:
--------------
array(0) {
}
Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error in
C:\xampp\htdocs\formatter\rubbish.php:25
Stack trace:
#0 C:\xampp\htdocs\formatter\rubbish.php(25): PDOStatement->fetchAll()
#1 {main}
thrown in C:\xampp\htdocs\formatter\rubbish.php on line 25
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=80267&edit=1