I recently encouraged a weird bug in PHP code. It as an internal block is try{}catch(){}omitted when using the PDOStatement c object foreach(){}. Here is my test code that causes this behavior:
<?php
$dsn = "mysql:dbname=test_pdo;host=localhost";
try {
$dbh = new PDO($dsn, 'root', "", array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("
CREATE TABLE IF NOT EXISTS `test` (
`id` int(10) unsigned NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$dbh->exec("TRUNCATE test");
$dbh->exec("
INSERT INTO test (id, name) VALUES
(1, 'JOHN DOE 1'), (2, 'JOHN DOE 2'),
(3, 'JOHN DOE 3'), (4, 'JOHN DOE 4')
");
echo "\nRECORDS CREATED\n";
$sth = $dbh->prepare("SELECT id FROM test WHERE id = ?");
foreach(array(2, 3, 4) as $id){
$sth->execute(array($id));
$i = 0;
foreach($sth as $row){
$num = $row['id'] - 1;
$sql = "UPDATE test SET name = 'JOHN DOE $num' WHERE id = {$row['id']}";
try{
echo "\nSENDING QUERY: $sql\n\n";
$dbh->exec($sql);
}catch(Exception $err){
$code = $err->getCode();
$msg = $err->getMessage();
if($err->getCode() != "23000"){
echo "THROWING EXCEPTION FROM INNER CATCH.\n ERROR CODE: $code\n";
throw $err;
}
echo <<<TXT
============ GOOD CATCH ============
ERROR CODE: $code
ERROR MSG: $msg
SQL: $sql
TXT;
}
}
}
}catch(Exception $e){
$code = $e->getCode();
$msg = $e->getMessage();
echo <<<TXT
============ WRONG CATCH ===========
ERROR CODE: $code
ERROR MSG: $msg
TXT;
}
This outputs something like this on my localhost (PHP 5.3.6-13ubuntu3.6 with Suhosin-Patch (cli)) and on the server (PHP 5.2.17):
RECORDS CREATED
SENDING QUERY: UPDATE test SET name = 'JOHN DOE 1' WHERE id = 2
============ GOOD CATCH ============
ERROR CODE: 23000
ERROR MSG: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'JOHN DOE 1' for key 'name'
SQL: UPDATE test SET name = 'JOHN DOE 1' WHERE id = 2
============ WRONG CATCH ===========
ERROR CODE: 23000
ERROR MSG: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'JOHN DOE 1' for key 'name'
I don’t know why, but when I change foreach($sth as $row){to foreach($sth->fetchAll() as $row){, everything works as expected:
RECORDS CREATED
SENDING QUERY: UPDATE test SET name = 'JOHN DOE 1' WHERE id = 2
============ GOOD CATCH ============
ERROR CODE: 23000
ERROR MSG: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'JOHN DOE 1' for key 'name'
SQL: UPDATE test SET name = 'JOHN DOE 1' WHERE id = 2
SENDING QUERY: UPDATE test SET name = 'JOHN DOE 2' WHERE id = 3
============ GOOD CATCH ============
ERROR CODE: 23000
ERROR MSG: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'JOHN DOE 2' for key 'name'
SQL: UPDATE test SET name = 'JOHN DOE 2' WHERE id = 3
SENDING QUERY: UPDATE test SET name = 'JOHN DOE 3' WHERE id = 4
============ GOOD CATCH ============
ERROR CODE: 23000
ERROR MSG: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'JOHN DOE 3' for key 'name'
SQL: UPDATE test SET name = 'JOHN DOE 3' WHERE id = 4
I found a mistake, or am I doing something wrong? Can someone confirm this behavior? thank
// EDIT
Upgrading from PHP version 5.3.10 seems to fix this problem. thanks for reference ...