2013-09-28 159 views
2

我使用codeigniter 2.1.4。 我將我的會話錄製到我的database1,並且我查詢了database2數據。但會話將不會發現database2表。我不想將會話記錄到database2codeigniter連接兩個數據庫

錯誤消息。

A Database Error Occurred 

Error Number: 1146 

Table 'database2.sessions' doesn't exist 

SELECT * FROM (`sessions`) WHERE `session_id` = 'fd384ac44b1fe1c073cfc23185esdfda' AND `user_agent` = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36' 

Filename: libraries/Session.php 

Line Number: 213 

我的database.php

$active_group = 'default'; 
$active_record = TRUE; 

$db['default']['hostname'] = 'localhost'; 
$db['default']['username'] = 'root'; 
$db['default']['password'] = 'password'; 
$db['default']['database'] = 'database1'; 
$db['default']['dbdriver'] = 'mysql'; 
$db['default']['dbprefix'] = ''; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = TRUE; 
$db['default']['cache_on'] = FALSE; 
$db['default']['cachedir'] = ''; 
$db['default']['char_set'] = 'utf8'; 
$db['default']['dbcollat'] = 'utf8_general_ci'; 
$db['default']['swap_pre'] = ''; 
$db['default']['autoinit'] = TRUE; 
$db['default']['stricton'] = FALSE; 

$db['database2']['hostname'] = 'localhost'; 
$db['database2']['username'] = 'root'; 
$db['database2']['password'] = 'password'; 
$db['database2']['database'] = 'database2'; 
$db['database2']['dbdriver'] = 'mysql'; 
$db['database2']['dbprefix'] = ''; 
$db['database2']['pconnect'] = TRUE; 
$db['database2']['db_debug'] = TRUE; 
$db['database2']['cache_on'] = FALSE; 
$db['database2']['cachedir'] = ''; 
$db['database2']['char_set'] = 'utf8'; 
$db['database2']['dbcollat'] = 'utf8_general_ci'; 
$db['database2']['swap_pre'] = ''; 
$db['database2']['autoinit'] = FALSE; 
$db['database2']['stricton'] = FALSE; 

config.php

$config['sess_cookie_name']  = 'ci_session'; 
$config['sess_expiration']  = 7200; 
$config['sess_expire_on_close'] = FALSE; 
$config['sess_encrypt_cookie'] = TRUE; 
$config['sess_use_database'] = TRUE; 
$config['sess_table_name']  = 'sessions'; 
$config['sess_match_ip']  = FALSE; 
$config['sess_match_useragent'] = TRUE; 
$config['sess_time_to_update'] = 300; 

這是我的查詢database2代碼。

function ip_get($ip) { 
    $db_rentcar = $this->load->database('rent_car', TRUE); 
    $db_rentcar->from('ip'); 
    $db_rentcar->where('ip_ip', $ip); 
    if($db_rentcar->count_all_results() == 1) { 
     $query = $db_rentcar->get_where('ip', array('ip_ip' => $ip)); 
     return $query->row_array(); 
    } else { 
     return array('ip_name' => 'user1', 'ip_type' => 1); 
    } 
    } 

我已嘗試將此代碼添加到Session.php

$this->CI->load->database('database1', TRUE); 

錯誤成爲。如何解決問題?謝謝。

An Error Was Encountered 

You have specified an invalid database connection group. 

回答

1

我找到了答案。

function simple_query($sql) 
{ 
    if (! $this->conn_id) 
    { 
     $this->initialize(); 
    } 

    $this->db_select(); // Add this code 

    return $this->_execute($sql); 
} 

參考here

3

你主動DB是您定義上次在database.php中 要連接下一個手動連接它或把它放在My_Controller如果你總是需要連接的最後一個。

$this->db2 = $this->CI->load->database('db2', TRUE); 

供您參考找到更詳細here

+1

把這個控制器工作對我來說'$這個 - > DB2 = $ this-> load-> database('db2',TRUE);' – Chad

2

連接到多個數據庫

如果您需要連接到多個數據庫同時你可以這樣做如下:

$DB1 = $this->load->database('group_one', TRUE); 
$DB2 = $this->load->database('group_two', TRUE); 

注:更改單詞「group_one」和「group_two」添加到您要連接的特定組名稱(或者您可以按照上面所述傳遞連接值)。

通過將第二個參數設置爲TRUE(布爾值),該函數將返回數據庫對象。

以這種方式連接時,您將使用對象名稱來發出命令,而不是本指南中使用的語法。換句話說,而不是發佈命令:

$this->db->query(); 
$this->db->result(); 
etc... 

實際上,您將使用:

$DB1->query(); 
$DB1->result(); 
etc... 

查看官方文檔https://www.codeigniter.com/userguide3/database/connecting.html#connecting-to-multiple-databases