php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40935 Error/Exception with PDO::fetch/fetchAll and disabled ATTR_EMULATE_PREPARES
Submitted: 2007-03-28 13:28 UTC Modified: 2007-04-15 16:50 UTC
From: phpbugs at filofox dot com Assigned:
Status: Closed Package: PDO related
PHP Version: 5.2.1 OS: Linux Debian Sarge 3.1
Private report: No CVE-ID:
 [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 '


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-04-03 18:49 UTC] tony2001@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.


 [2007-04-10 10:40 UTC] phpbugs at filofox dot com
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();

?>
 [2007-04-15 16:50 UTC] iliaa@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Fri Apr 18 10:03:03 2014 UTC