PDO prepare silently fails

Posted by Wabbitseason on Stack Overflow See other posts from Stack Overflow or by Wabbitseason
Published on 2010-04-07T20:56:36Z Indexed on 2010/04/07 23:03 UTC
Read the original article Hit count: 296

I'm experimenting with PHP's session_set_save_handler and I'd like to use a PDO connection to store session data.

I have this function as a callback for write actions:

function _write($id, $data) {
    logger('_WRITE ' . $id . ' ' . $data);
    try {
        $access = time();
        $sql = 'REPLACE INTO sessions SET id=:id, access=:access, data=:data';
        logger('This is the last line in this function that appears in the log.');
        $stmt = $GLOBALS['db']->prepare($sql);
        logger('This never gets logged! :(');
        $stmt->bindParam(':id', $id, PDO::PARAM_STR);
        $stmt->bindParam(':access', $access, PDO::PARAM_INT);
        $stmt->bindParam(':data', $data, PDO::PARAM_STR);
        $stmt->execute();
        $stmt->closeCursor();
        return true;
    } catch (PDOException $e) {
        logger('This is never executed.');
        logger($e->getTraceAsString());
    }
}

The first two log messages always show up, but the third one right after $stmt = $GLOBALS['db']->prepare($sql) never makes it to the log file and there's no trace of an exception either.

The sessions db table remains empty.

The log message from the _close callback is always present.

Here's how I connect to the database:

$db = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

I have PHP 5.2.10.

I tried to simply run $GLOBALS['db']->exec($sql) with a "manually prepared" $sql content, but it still failed silently. The query itself is all right I was able to execute it via the db console.

© Stack Overflow or respective owner

Related posts about php

Related posts about pdo