2016-10-03 85 views
0

我正在codeigniter應用程序和應用程序安裝在本地主機和web服務器上,但實際上我想這是數據應該插入本地數據庫和web服務器數據庫使用本地主機?codeigniter使用本地主機將數據插入兩個數據庫?

如何在database.php文件中連接web服務器數據庫以及如何在CI_Controller函數中使用web服務器連接?

回答

1

你可以看看這個帖子Codeigniter - multiple database connections 但基本上是這樣的 它添加到database.php中

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

,你可以從模型這樣

function my_model_method() 
{ 
    $otherdb = $this->load->database('otherdb', TRUE); // the TRUE paramater tells CI that you'd like to return the database object. 

    $query = $otherdb->select('first_name, last_name')->get('person'); 
    var_dump($query); 
} 
+0

請告訴如何一次插入兩個數據庫? –

+0

我不明白你發給我的插入示例? –

0

可以稱之爲這樣做

function my_model_method() 
{ 
$otherdb = $this->load->database('otherdb', TRUE); 
$data= array('name'=>'John'); 
$database1=$this->db->insert('students', $data); //default db 
$database2 = $otherdb->insert('students',$data); //the other db 
} 
+0

數據插入到活動數據庫而非本地主機數據庫? –

+0

我不知道你對「Live Database」的意思,你可以在任何你想要的地方配置數據庫服務器,可以是本地主機或者從亞馬遜網站上下載數據庫服務器$ db ['db'] ['hostname'] =「localhost」; 或 $ db ['otherdb'] ['hostname'] =「mydbinstance.abcdefghijkl.us-east-1.rds.amazonaws.com」;' –

相關問題