3
votes

I am trying to do an OTH application and i am getting this error

A Database Error Occurred

Error Number: 1054

Unknown column 'data' in 'field list'

SELECT data FROM ci_sessions WHERE id = 'dd8fddaabb45c365fbf27a6fdc5ea60d59f569e4' AND ip_address = '::1'

Filename: libraries/Session/drivers/Session_database_driver.php

Line Number: 160

This is Session_database_driver.php

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)
            {
                $this->_fingerprint = md5('');
                return '';
            }

            // PostgreSQL's variant of a BLOB datatype is Bytea, which is a
            // PITA to work with, so we use base64-encoded data in a TEXT
            // field instead.
            $result = ($this->_platform === 'postgre')
                ? base64_decode(rtrim($result->data))
                : $result->data;

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

        $this->_fingerprint = md5('');
        return '';
}
3
@OfirBaruch I tried that but its not working. Actually over there some table name missing problem was there. - Amogh
If you change select('data') to select('user_data') what happens? - Ofir Baruch
@OfirBaruch Unknown column 'id' in 'where clause' SELECT user_data FROM ci_sessions WHERE id = '0c37a2a2094055181b386efea510a21068d71e4d' AND ip_address = '::1' - Amogh
I think I understand the problem - Ofir Baruch

3 Answers

4
votes

According to CI manual regarding Sessions the basic format of the ci_sessions table is:

CREATE TABLE IF NOT EXISTS  `ci_sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(45) DEFAULT '0' NOT NULL,
    user_agent varchar(120) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id),
    KEY `last_activity_idx` (`last_activity`)
);

In your php code you're using different fields:

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

So, by altering that code to:

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

Your code should work.

2
votes

I think the right solution is to check if your DB is correctly created.

According to Documentation for CI 3.0.0 the correct database would be:

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

I dont agree on changing CI drivers files

1
votes

I got the same problem, i already changed...

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

to ->>>>>

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

the problem will exist if i do this in my config.ph

$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;