Bug #80267 [Opn]: PDO throws an exception when using fetchAll on non-select stmt with emulation

From: Date: Wed, 21 Oct 2020 14:48:41 +0000
Subject: Bug #80267 [Opn]: 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-229840@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 User updated by: tekiela246 at gmail dot com Reported by: tekiela246 at gmail dot com -Summary: PDO throws an exception when using fetchAll on non-select stmt with no emulatio +Summary: PDO throws an exception when using fetchAll on non-select stmt with emulation Status: Open Type: Bug Package: PDO MySQL Operating System: Windows 10 PHP Version: 7.3.23 Block user comment: N Private report: N New Comment: It happens when PDO::ATTR_EMULATE_PREPARES = true Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#229840) next »