PDO prepares silently

I am experimenting with PHP session_set_save_handler and I would 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 are always displayed, and the third immediately after $stmt = $GLOBALS['db']->prepare($sql) never gets into the log file and does not contain any traces of an exception.

The db session table remains empty.

The log message from the _close always present.

This is 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 just running $GLOBALS['db']->exec($sql) with the "manually prepared" $sql content, but it still crashed. The request itself is right, I was able to execute it through the db console.




Edit:

After VolkerK identified the problem, I found this article that explains the cause of these strange phenomena. Perhaps this may be informative for others.




2nd edit:

The least painful, magical solution is that I had to add a function call below to the end of my front-control file (main index.php):

 session_write_close(); 
+7
php pdo session session-set-save-handler
Apr 07 '10 at
source share
3 answers

My bets are included: $ GLOBALS ['db'] is not installed or is not an instance of pdo (more?), And therefore PHP Fatal error: Call to a member function prepare() on a non-object occurs PHP Fatal error: Call to a member function prepare() on a non-object , and php issues messages.

 $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.'); if ( !isset($GLOBALS['db']) ) { logger('there is no globals[db]'); return; } else if ( !is_object($GLOBALS['db']) ) { logger('globals[db] is not an object'); return; } else if ( !($GLOBALS['db'] instanceof PDO) ) { logger('globals[db] is not a PDO object'); return; } else { logger('globals[db] seems ok'); } $stmt = $GLOBALS['db']->prepare($sql); logger('This never gets logged! :('); 
+4
Apr 07 '10 at 22:05
source share

PDO might not recognize REPLACE INTO syntax. If the basic database access library does not support directly prepared statements, PDO imitates them and may not have REPLACE INTO in its list of possible types of statements.

Try checking $stmt->errorCode() right after the call to prepare?

If it is mysql, you can try to rewrite the prepared statement as follows:

 INSERT INTO sessions (id, access, data) VALUES(:id, :access, :data) ON DUPLICATE KEY UDPATE access=VALUES(access), data=VALUES(data); 

and see if that helps you further.

+1
Apr 07 2018-10-22T00:
source share

I feel that access to data resources is not available through $GLOBALS[] . Something about how links are handled or something like that. If you are in the mood for humor, try this to declare your function:

 function _write($id, $data) { global $db; logger('_WRITE ' . $id . ' ' . $data); try { 

And instead:

 $stmt = $GLOBALS['db']->prepare($sql); 

to try

 $stmt = $db->prepare($sql); 

You can also try to catch a plain old Exception instead of PDOException s; this may be due to this.

Hope this helps!

0
Apr 08 '10 at 3:27
source share



All Articles