[英]How to switch to different default database in CodeIgniter?
我在數據庫配置文件中設置了兩個不同的連接。 一旦我連接到數據庫之一,如果我連接到第二個數據庫仍然連接不會更改。 我找不到表錯誤。
$this->load->database(); //connecting to default db
$this->db->query('SELECT * from user'); //user table in default db and no error
$this->load->database('second_db');//connecting to second db
$this->db->load('SELECT * from statistic'); //table exists in second db but getting error
//The same work if I comment the first two lines
你可以嘗試這種方式:
$this->load->database('second_db', TRUE);
並在database.php中為'pconnect'設置FALSE:默認值為:
$db['default']['pconnect'] = FALSE;
第二個:
$db['second_db']['pconnect'] = FALSE;
我自己做了。
$this->load->database(); //connecting to default db
$this->db->query('SELECT * from user'); //user table in default db and no error
$this->load->database('second_db',FALSE,TRUE);//connecting to second db
$this->db->load('SELECT * from statistic'); //table exists in second db but getting error
//The same work if I comment the first two lines
唯一的變化是加載第二個數據庫時需要傳遞兩個額外的參數。 第一個FALSE - 不返回連接對象第二個TRUE - 將Active記錄更改為已加載的DB
如果你使用
$this->load->database('second_db', TRUE);
你必須使用喜歡
$db = $this->load->database('second_db', TRUE);
因為第二個參數true返回完整對象現在$ db是您的db對象,您可以將$ db分配給類變量
$this->second_db = $this->load->database('second_db', TRUE);
$this->second_db->query();
最后使用Codeigniter 2.1.0為我工作:
$second_db = $this->load->database('second_group',TRUE,FALSE);
使組不斷設置'pconnect'為:
$db['second_group']['pconnect'] = FALSE;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.