Search code examples
phppdophp-5.2

PHP PDO & transactions - different behavior on PHP 5.4 and PHP 5.2.13


is there some significant difference between PHP 5.4 and 5.2.13 for the following code? It works with PHP 5.4 (& mysql 5.1.62), no error is shown and query is executed. However with 5.2.13 (& mysql 5.1.3.0) it doesn't show any error (it doesn't fall in to the catch block) but the data are not inserted into the database

 $db = new PDO(DHOST, DUSER, DPASS, array(PDO::ATTR_PERSISTENT => true, PDO::ATTR_EMULATE_PREPARES => true));
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);

 try {
        $db->beginTransaction();
        $db->exec($insertString);
        $db->commit();
    } catch (Exception $e) {
        try {
            $db->rollBack();
            throwDbError($handler->errorInfo());
        } catch (Exception $e2) {
            ...
        }
    }

If I ommit the lines with beginTransaction and commit, it works in PHP 5.2 as well but I need the transactions. Both tables in mysql databases are using InnoDB


Solution

  • The error mode is not set to throw exceptions. Change:

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
    

    To:

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    There should be no discernable difference between the two PHP versions.