Bug #80458 [PATCH]: PDOStatement::fetchAll() throws for upsert queries

From: Date: Wed, 02 Dec 2020 22:57:09 +0000
Subject: Bug #80458 [PATCH]: PDOStatement::fetchAll() throws for upsert queries
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230804@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80458&edit=1

 ID:                 80458
 Patch added by:     cmb@php.net
 Reported by:        php dot net dot 20 dot 12 dot 2016 at 0pe dot de
 Summary:            PDOStatement::fetchAll() throws for upsert queries
 Status:             Open
 Type:               Bug
 Package:            PDO MySQL
 Operating System:   Arch Linux
 PHP Version:        7.4.13
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2020-12-02 15:54:05] nikic@php.net

Related To: Bug #80472

------------------------------------------------------------------------
[2020-12-01 19:06:29] 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



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80458&edit=1


Thread (3 messages)

« previous php.bugs (#230804) next »