PDO静默准备失败 [英] PDO prepare silently fails

查看:91
本文介绍了PDO静默准备失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试验PHP的session_set_save_handler,我想使用PDO连接来存储会话数据.

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());
    }
}

总是显示前两个日志消息,但是紧随$stmt = $GLOBALS['db']->prepare($sql)之后的第三个日志消息永远不会出现在日志文件中,也没有任何异常的踪迹.

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.

会话数据库表保持空白.

_close回调中的日志消息始终存在.

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);

我有PHP 5.2.10.

I have PHP 5.2.10.

我试图简单地运行带有手动准备的" $sql内容的$GLOBALS['db']->exec($sql),但是它仍然默默地失败了.我可以通过数据库控制台执行查询.

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.

在VolkerK确定问题之后,我发现了

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.

最痛苦,最神奇的解决方案是,我必须将以下函数调用添加到前端控制器(主index.php)文件的末尾:

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:

session_write_close();

推荐答案

我的赌注是:$ GLOBALS ['db']未设置或pdo的实例(不再吗?),因此出现PHP Fatal error: Call to a member function prepare() on a non-object php救了出来.

My bets are on: $GLOBALS['db'] is not set or not an instance of pdo (anymore?) and therefor a PHP Fatal error: Call to a member function prepare() on a non-object occurs and php bails out.

$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! :(');

这篇关于PDO静默准备失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆