PDO prepare silently fails

Ragou

New Member
I'm experimenting with PHP's \[code\]session_set_save_handler\[/code\] and I'd like to use a PDO connection to store session data.I have this function as a callback for write actions:\[code\]function _write($id, $data) { logger('_WRITE ' . $id . ' ' . $data); try { $access = time(); $sql = 'REPLACE INTO sessions SET id=:id, access=:access, data=http://stackoverflow.com/questions/2595860/: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()); }}\[/code\]The first two log messages always show up, but the third one right after \[code\]$stmt = $GLOBALS['db']->prepare($sql)\[/code\] 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 \[code\]_close\[/code\] callback is always present.Here's how I connect to the database:\[code\]$db = new PDO('mysql:host=' . DBHOST . ';dbname=' . DBNAME, DBUSER, DBPASS);$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);\[/code\]I have PHP 5.2.10.I tried to simply run \[code\]$GLOBALS['db']->exec($sql)\[/code\] with a "manually prepared" \[code\]$sql\[/code\] content, but it still failed silently. The query itself is all right I was able to execute it via the db console.Edit:After VolkerK has identified the problem I've found this article which explains the reason behind this weird phenomena. Perhaps it could be informative to others as well.2nd edit:The least painful, magic solution is that I had to add the below function call to the very end of my front controller (the main index.php) file:\[code\]session_write_close();\[/code\]
 
Top