php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80267 PDO throws an exception when using fetchAll on non-select stmt with emulation
Submitted: 2020-10-21 14:46 UTC Modified: 2020-12-08 09:01 UTC
From: tekiela246 at gmail dot com Assigned: nikic (profile)
Status: Closed Package: PDO MySQL
PHP Version: 7.3.23 OS: Windows 10
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: tekiela246 at gmail dot com
New email:
PHP Version: OS:

 

 [2020-10-21 14:46 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-10-21 14:48 UTC] 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
 [2020-10-21 14:48 UTC] tekiela246 at gmail dot com
It happens when PDO::ATTR_EMULATE_PREPARES = true
 [2020-12-08 09:01 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 01:01:28 2024 UTC