php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80458 PDOStatement::fetchAll() throws for upsert queries
Submitted: 2020-12-01 19:06 UTC Modified: 2020-12-02 15:53 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:0 (0.0%)
From: php dot net dot 20 dot 12 dot 2016 at 0pe dot de Assigned:
Status: Closed Package: PDO MySQL
PHP Version: 7.4.13 OS: Arch Linux
Private report: No CVE-ID: None
 [2020-12-01 19:06 UTC] php dot net dot 20 dot 12 dot 2016 at 0pe dot de
Description:
------------
Using PDOStatement::fetch() or PDOStatement::fetachAll() after a query without resulting data (INSERT, DELETE and so on) results in throw of PDOException.

2 problems:

1.: Before PHP 7.4.13 fetch(All) returned false. Throwing an exception here seems wrong to me.

2.: Exception text is misleading. It says that there are other unbuffered queries even when there are no queries made before.

Exception text: "Uncaught PDOException: SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active.  Consider using PDOStatement::fetchAll().  Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute"

Test script:
---------------
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Connect
$pdoLink = new PDO("mysql:dbname=test;host=localhost;charset=UTF8", 'root', 'root');
$pdoLink->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
$pdoLink->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdoLink->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_NATURAL);
$pdoLink->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$pdoLink->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$pdoLink->setAttribute(PDO::ATTR_AUTOCOMMIT, true);
$pdoLink->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

// Query
$stmt = 'INSERT INTO `pdotest_user` (`username`) VALUES (:username)';
$pdoStmt = $pdoLink->prepare($stmt);
$pdoStmt->execute([':username' => 'Testman']);

// Result
$resArr = [];
while (($row = $pdoStmt->fetch(PDO::FETCH_ASSOC)) !== false)   // Exception thrown here
	$resArr[] = $row;

// Cleanup
$pdoStmt->closeCursor();

print_r($resArr);

Expected result:
----------------
fetch/fetchAll should return false if there is no data for the query


Patches

Pull Requests

Pull requests:

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-12-02 15:53 UTC] nikic@php.net
-Summary: Exception thrown instead of return false +Summary: PDOStatement::fetchAll() throws for upsert queries
 [2020-12-02 22:57 UTC] cmb@php.net
The following pull request has been associated:

Patch Name: Bug #80458 PDOStatement::fetchAll() throws for upsert queries
On GitHub:  https://github.com/php/php-src/pull/6482
Patch:      https://github.com/php/php-src/pull/6482.patch
 [2020-12-04 16:02 UTC] nikic@php.net
Automatic comment on behalf of tekiela246@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a83cc03c138b8cf27a840bd7cd913eb7050e55ba
Log: Fixed bug #80458
 [2020-12-04 16:02 UTC] nikic@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 05 22:01:29 2024 UTC