|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2007-03-28 13:28 UTC] phpbugs at filofox dot com
 Description:
------------
When using PDO with MySQL, if ATTR_EMULATE_PREPARES is disabled, an exception (or error if not using ERRMODE_EXCEPTION) is thrown if you try to fetch() or fetchAll() a null result set (i.e. after INSERT, UPDATE etc.). However, if ATTR_EMULATE_PREPARES is enabled, the exception/error is not triggered.
Clearly fetch on a null result set is redundant, but either it should throw an exception/error or it shouldn't, regardless of the state of ATTR_EMULATE_PREPARES. 
Reproduce code:
---------------
$db = new PDO( $dsn, $username, $password );
// enables exceptions
$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
// disable emulation <--- this triggers the problem
$db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false );
$sql = 'INSERT INTO TestTable VALUES ( NULL )';
$query = $db->prepare( $sql );
if( $query->execute() )
{
	$results = $query->fetchAll( PDO::FETCH_ASSOC );
}
$query->closeCursor();
Expected result:
----------------
Either an exception/error should be thrown in both cases, or neither.
Actual result:
--------------
'PDOException' :: 'SQLSTATE[HY000]: General error: 2053 '
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 22:00:01 2025 UTC | 
Revised test code: ================== <?php $db = new PDO( $dsn, $username, $password ); // enables exceptions $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // disable emulation <--- this triggers the problem $db->setAttribute( PDO::ATTR_EMULATE_PREPARES, false ); // Create a dummy table $db->exec("CREATE TABLE IF NOT EXISTS TestTable (id INT)"); $sql = 'INSERT INTO TestTable VALUES ( NULL )'; $query = $db->prepare( $sql ); if( $query->execute() ) { $results = $query->fetchAll( PDO::FETCH_ASSOC ); } $query->closeCursor(); ?>