Codeigniter错误会话DB

时间:2014-11-16 18:32:43

标签: php codeigniter

将Codeigniter升级到版本3.0后,尝试呼叫任何控制器后都会收到错误数据库:

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `id` = 'd1d384b0ceed0bd72fa210337acc666aab1a04e5'' at line 2

SELECT `data` WHERE `id` = 'd1d384b0ceed0bd72fa210337acc666aab1a04e5'

Filename: libraries/Session/drivers/Session_database_driver.php

当我重新加载页面时,我再次使用其他生成的哈希会话。怎么解决?

config.php我设置了会话存储:$config['sess_driver'] = 'database';

我查看了文件哪里出错(第138行):

public function read($session_id)
    {
        if ($this->_get_lock($session_id) !== FALSE)
        {
            // Needed by write() to detect session_regenerate_id() calls
            $this->_session_id = $session_id;

            $this->_db
                ->select('data')
                ->from($this->_config['save_path'])
                ->where('id', $session_id);

            if ($this->_config['match_ip'])
            {
                $this->_db->where('ip_address', $_SERVER['REMOTE_ADDR']);
            }

            if (($result = $this->_db->get()->row()) === NULL) // Line 138
            {
                $this->_fingerprint = md5('');
                return '';
            }

            $this->_fingerprint = md5(rtrim($result->data));
            $this->_row_exists = TRUE;
            return $result->data;
        }

        $this->_fingerprint = md5('');
        return '';
    }

5 个答案:

答案 0 :(得分:25)

我遇到了同样的问题,我花了一段时间才找到解决方案。从表面上看,指令告诉您只需加载会话驱动程序,一切都很酷,但如果您查看CodeIgniter文档的数据库驱动程序部分,您会发现需要配置$ config ['sess_save_path']变量在你的config.php文件中。例如:

 $config[‘sess_save_path’] = ‘ci_sessions’

http://www.codeigniter.com/userguide3/libraries/sessions.html#initializing-a-session

当然,您还必须在数据库中设置ci_sessions表,但这解决了我丢失的表名。

答案 1 :(得分:10)

使用codeigniter 3.0:

在配置文件中:

$config['sess_driver'] = 'database';
$config['sess_save_path'] = 'ci_sessions';

在MySQL数据库中:

CREATE TABLE IF NOT EXISTS `ci_sessions` (
    `id` varchar(40) NOT NULL,
    `ip_address` varchar(45) NOT NULL,
    `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
    `data` blob NOT NULL,
    PRIMARY KEY (id),
    KEY `ci_sessions_timestamp` (`timestamp`));

字段名称与2.0版本不同

答案 2 :(得分:7)

使用Codeigniter版本3

编辑配置文件并将会话驱动程序设置为“database”,将 sess_save_path 设置为表名

$config['sess_driver'] = 'database';//enable session with db
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';//database table name
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

对于MySql

CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(40) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        KEY `ci_sessions_timestamp` (`timestamp`)
);

对于PostgreSQL:

    CREATE TABLE "ci_sessions" (
            "id" varchar(40) NOT NULL,
            "ip_address" varchar(45) NOT NULL,
            "timestamp" bigint DEFAULT 0 NOT NULL,
            "data" text DEFAULT '' NOT NULL
    );

CREATE INDEX "ci_sessions_timestamp" ON "ci_sessions" ("timestamp");

Official Codeigniter Documentation for Session database driver

答案 3 :(得分:2)

我使用CI 3而只是改变:

$config['sess_save_path']           = 'ci_sessions';

作品.. =)

答案 4 :(得分:0)

选择'数据' FROM TABLE_NAME WHERE' id' =' d1d384b0ceed0bd72fa210337acc666aab1a04e5'

您缺少表名,而您的代码包含特殊字符。