Bug #80267 [NEW]: PDO throws an exception when using fetchAll on non-select stmt with no emulatio

From: Date: Wed, 21 Oct 2020 14:46:47 +0000
Subject: Bug #80267 [NEW]: PDO throws an exception when using fetchAll on non-select stmt with no emulatio
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-229839@lists.php.net to get a copy of this message
From: tekiela246 at gmail dot com Operating system: Windows 10 PHP version: 7.3.23 Package: PDO MySQL Bug Type: Bug Bug description:PDO throws an exception when using fetchAll on non-select stmt with no emulatio 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 bug report at https://bugs.php.net/bug.php?id=80267&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=80267&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=80267&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=80267&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=80267&r=needscript Try newer version: https://bugs.php.net/fix.php?id=80267&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=80267&r=support Expected behavior: https://bugs.php.net/fix.php?id=80267&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=80267&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=80267&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=80267&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=80267&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=80267&r=dst IIS Stability: https://bugs.php.net/fix.php?id=80267&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=80267&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=80267&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=80267&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=80267&r=mysqlcfg

« previous php.bugs (#229839) next »