2012-06-27 21 views
5

我有一個CodeIgniter和一些Mysql數據庫。但知道我想設置另一個數據庫的工作,並在一些行動後禁用它。例如,我有功能make_some_actions_with_db(),現在我需要有類似的東西:如何使用CodeIgniter加載另一個數據庫?

public function action() 
{ 
//load db 
make_some_actions_with_db(); 
//disable db 
} 

所以,現在我需要知道如何設置新的默認數據庫,我怎麼可以設置其他(第一) D b。謝謝。

更新: 它不工作:

public function update_record_insert_test() 
    { 
     if ($this->facebook->getUser()) 
     { 
      $another_db_settings['hostname'] = 'localhost'; 
      $another_db_settings['username'] = 'root'; 
      $another_db_settings['password'] = ''; 
      $another_db_settings['database'] = 'name'; 
      $another_db_settings['dbdriver'] = "mysql"; 
      $another_db_settings['dbprefix'] = ""; 
      $another_db_settings['pconnect'] = TRUE; 
      $another_db_settings['db_debug'] = TRUE; 
      $another_db_settings['cache_on'] = FALSE; 
      $another_db_settings['cachedir'] = ""; 
      $another_db_settings['char_set'] = "utf8"; 
      $another_db_settings['dbcollat'] = "utf8_general_ci"; 
      $this->load->database($another_db_settings); 

      $_POST['id']='AccountPagesView.a_book/1000'; 
      $_POST['value']='test'; 
      $_POST['old_value']='not'; 
      $welcome=new Welcome(); 
      $welcome->update_record(); 
     } 
     else 
     { 
      $url=$this->facebook->getLoginUrl(array('next' => base_url().'update_record_insert_test')); 
      redirect($url); 
     } 
    } 

回答

9

在你config/database.php您的數據庫配置爲 '默認' 組,就像這樣:

$db['default']['hostname'] = 'host'; 
$db['default']['username'] = 'username'; 
$db['default']['password'] = 'password'; 
$db['default']['database'] = 'database'; 

這可以讓你指定其他組如此:

$db['second']['hostname'] = 'host'; 
$db['second']['username'] = 'username'; 
$db['second']['password'] = 'password'; 
$db['second']['database'] = 'database'; 

$db['third']['hostname'] = 'host'; 
$db['third']['username'] = 'username'; 
$db['third']['password'] = 'password'; 
$db['third']['database'] = 'database'; 

然後,您可以通過使用連接到另一個數據庫:

$defaultDB = $this->load->database('default', TRUE); 
$secondDB = $this->load->database('second', TRUE); 

通過傳遞true作爲第二值,它會返回數據庫對象,讓您安全地使用同樣的方法對每個數據庫,就像這樣:

$default->get('table'); 
$second->get('different_table'); 
+0

到另一個變量的新連接現在,我讓使用$所有請求,這 - > DB->得到(.. ),並且我不能更改此代碼,但是有相同的db和相同的表,我需要更改默認db這一個。我如何做到這一點?例如,現在我有「db1」數據庫,第二個是「db1_copy」 - 我需要類似$ this-> db - > ...的所有請求都將使用「db1_copy」執行。 – user1477886

+0

如果您可以更改配置,請將'default'組的詳細信息更改爲'db1_copy'的詳細信息。這應該工作? – cchana

+0

不,我剛剛嘗試過,它不起作用。 – user1477886

0

如果要更改默認的數據庫連接(我不推薦),你可以這樣做:

$another_db_settings['hostname'] = ''; 
$another_db_settings['username'] = ''; 
$another_db_settings['password'] = ''; 
$another_db_settings['database'] = ''; 
$another_db_settings['dbdriver'] = "mysql"; 
$another_db_settings['dbprefix'] = ""; 
$another_db_settings['pconnect'] = TRUE; 
$another_db_settings['db_debug'] = TRUE; 
$another_db_settings['cache_on'] = FALSE; 
$another_db_settings['cachedir'] = ""; 
$another_db_settings['char_set'] = "utf8"; 
$another_db_settings['dbcollat'] = "utf8_general_ci"; 
$this->load->database($another_db_settings); 

然後將新的數據庫可以通過這個 - $>數據庫訪問,但我會建議做什麼建議和cchana通過分配

$new_db = $this->load->database($another_db_settings, TRUE); 
+0

您的建議無效。請檢查更新的問題。 – user1477886

+0

你能展示歡迎課嗎?在這種情況下,我不明白爲什麼你需要另一個數據庫連接。 – marshall

+0

Welcome類中的update_record()函數對主數據庫執行一些操作。我需要測試它,並且我想使用它作爲主分貝的副本。 – user1477886

相關問題