问题
I use codeigniter 2.1.4
.
I record my session to my database1
, and I query database2
data. But session will not found database2
table. I don't want to record session to database2
.
The error message.
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
My 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;
My 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;
That is my query database2
code.
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);
}
}
I have try to add this code to Session.php
.
$this->CI->load->database('database1', TRUE);
The error become. How to solve the problem? Thanks.
An Error Was Encountered
You have specified an invalid database connection group.
回答1:
Your active db is the last one that you define last in database.php To connect next one connect it manually or put it in My_Controller if you always need to connect.
$this->db2 = $this->CI->load->database('db2', TRUE);
For your reference find more detail here
回答2:
Connecting to Multiple Databases
If you need to connect to more than one database simultaneously you can do so as follows:
$DB1 = $this->load->database('group_one', TRUE);
$DB2 = $this->load->database('group_two', TRUE);
Note: Change the words "group_one" and "group_two" to the specific group names you are connecting to (or you can pass the connection values as indicated above).
By setting the second parameter to TRUE (boolean) the function will return the database object.
When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:
$this->db->query();
$this->db->result();
etc...
You will instead use:
$DB1->query();
$DB1->result();
etc...
view official documentation https://www.codeigniter.com/userguide3/database/connecting.html#connecting-to-multiple-databases
回答3:
I found the answer.
function simple_query($sql)
{
if ( ! $this->conn_id)
{
$this->initialize();
}
$this->db_select(); // Add this code
return $this->_execute($sql);
}
Reference here
来源:https://stackoverflow.com/questions/19063324/codeigniter-connect-two-database