尝试catch块不使用pdo语句和foreach

时间:2012-03-08 21:38:03

标签: php pdo try-catch pdostatement

最近我鼓励PHP代码中出现奇怪的错误。当使用带有try{}catch(){}的PDOStatement对象时,省略了内部foreach(){}块。这是我的测试代码,它会触发此行为:

<?php
/* 
// THIS WORKS AS EXPECTED, JUST UNCOMMENT
try{
    for($i = 0; $i < 3; $i++){
        try{
            echo "\nTHROWING EXCEPTION\n\n";
            throw new Exception("AAAAAAAAAAAAAAAAAAAAAAAAA");
            echo "EXCETION THROWN (SHOULD NOT SHOW)\n";
        }catch(Exception $err){
            echo "========= INNER CATCH =========\n";
            echo $err->getMessage() . "\n";
        }
    }
}catch(Exception $e){
    echo "=========== OUTER CATCH ===========\n";
    die($e->getMessage() . "\n");
}
die("\nEVERYTHING OK\n");
*/
$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;
            // this sql should throw 2300 error duplicate key
            $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();
                //duplicate key error?
                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;
}

这会在我的localhost(带有Suhosin-Patch(cli)的PHP 5.3.6-13ubuntu3.6)和服务器(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'

不知道为什么,但当我将foreach($sth as $row){更改为foreach($sth->fetchAll() as $row){时,每个人都按预期工作:

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

我发现了一个错误,或者我做错了什么?有人可以确认这种行为吗? THX

// 编辑

将PHP版本升级到5.3.10似乎解决了这个问题。求救......

1 个答案:

答案 0 :(得分:0)

不,从PDO发回的结果不是您可以直接迭代的结果。它不是PDO异常,当你尝试这样做时它是一个PHP - 它不是一个数组或一个有迭代器的对象。

当你获取它时会创建一个在foreach循环中有效的数组。