From:             [EMAIL PROTECTED]
Operating system: Any
PHP version:      5.2.6RC2
PHP Bug Type:     PDO related
Bug description:  Unexpected exception thrown in foreach() statement

Description:
------------
I'm using the SQL SELECT result of an PDO query directly in an foreach()
loop (via it's iterator capability).

Within this loop I'm doing INSERTs and they violate integrity constraints
and thus PDO throws an exception.

I'm catching and basically suppressing the exception. However when the
foreach() loop is finished, an exception gets thrown with the line where
the foreach() statement is declared.

It seems internally the thrown but silently ignored exception has queued
and suddenly being let loose when the foreach() loop finishes.

It doesn't matter whether the SELECT-statement fetches the data from the
table where it is inserted or not, this is just done to make the example
simpler.

I've also tested with php5.3-win32-200803170030 and it's the same
behavior.

It seems to be an similar issue like #39345, but in my case I'm using the
mysql driver and the exception is within my PHP source.

Reproduce code:
---------------
<?php
    $oPdo = new PDO('mysql:host=db01;dbname=test', 'test', 'test');
    $oPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $oPdo->query('DROP TABLE IF EXISTS test');
    $sSqlCreate = <<<SQL
CREATE TABLE `test` (
  `a` bigint(20) unsigned NOT NULL,
  `b` bigint(20) unsigned NOT NULL,
  UNIQUE KEY `unique_ref` (`a`,`b`)
) ENGINE=MyISAM;
SQL;

    $oPdo->query($sSqlCreate);
    $oPdo->query('TRUNCATE test');
    $oPdo->query('INSERT INTO test(a, b) VALUES(1, 1)');

    $oResult = $oPdo->query('SELECT * FROM test');
    foreach ($oResult as $aRow) {
        try {
            $oPdo->query('INSERT INTO test(a, b) VALUES(1, 1)');
        } catch (Exception $e) { }
    }
?>


Expected result:
----------------
Don't throw an exception:

$ php reproduce_pdo_bug.php
# 0
# 1
$

Actual result:
--------------
$ php reproduce_pdo_bug.php
# 0
# 1

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062
Duplicate entry '1-1' for key 1 in reproduce_pdo_bug.php on line 20

Call Stack:
    0.0002      58460   1. {main}() reproduce_pdo_bug.php:0


-- 
Edit bug report at http://bugs.php.net/?id=44454&edit=1
-- 
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=44454&r=trysnapshot52
Try a CVS snapshot (PHP 5.3): 
http://bugs.php.net/fix.php?id=44454&r=trysnapshot53
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=44454&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=44454&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=44454&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=44454&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=44454&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=44454&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=44454&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=44454&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=44454&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=44454&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=44454&r=globals
PHP 4 support discontinued:   http://bugs.php.net/fix.php?id=44454&r=php4
Daylight Savings:             http://bugs.php.net/fix.php?id=44454&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=44454&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=44454&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=44454&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=44454&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=44454&r=mysqlcfg

Reply via email to