2011-11-25 93 views
34

我必須從主數據庫檢索MySQL數據庫信息,然後連接到該數據庫並獲取一些記錄。Codeigniter - 多個數據庫連接

我的意思是拿着一個數據庫我想加載另一個數據庫。

Codeigniter可以嗎?現在我在我的模型中使用了以下幾行代碼。

function connectDb($credential) 
{ 

    $config['hostname'] = $credential['server']; 
    $config['username'] = $credential['username']; 
    $config['password'] = $credential['password']; 
    $config['database'] = $credential['database']; 
    $config['dbdriver'] = "mysql"; 
    $config['dbprefix'] = ""; 
    $config['pconnect'] = FALSE; 
    $config['db_debug'] = TRUE; 
    $config['cache_on'] = FALSE; 
    $config['cachedir'] = ""; 
    $config['char_set'] = "utf8"; 
    $config['dbcollat'] = "utf8_general_ci"; 

    $DB2=$this->load->database($config); 

    $DB2->db->select('first_name,last_name'); 
    $query = $DB2->db->get('person'); 
    print_r($query); 

} 

它不工作有沒有其他辦法?

+0

定義「不工作」 –

回答

60

你應該提供'應用程序/配置/ database.php'

通常情況下,你將設置default數據庫組第二數據庫的信息,像這樣:

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "root"; 
$db['default']['password'] = ""; 
$db['default']['database'] = "database_name"; 
$db['default']['dbdriver'] = "mysql"; 
$db['default']['dbprefix'] = ""; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = FALSE; 
$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['default']的陣列中提供設置。

然後,您可以在新數組中添加另一個數據庫 - 我們稱之爲'otherdb'。

$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; 

現在,實際使用第二個數據庫,你要發送連接到另一個variabel,你可以在模型中使用:應該這樣做

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); 
} 

。 用於連接到多個數據庫中的文檔可以在這裏找到:http://codeigniter.com/user_guide/database/connecting.html

+0

我會試試,謝謝 – zamebit

6

雖然看着你的代碼,我看到錯誤的唯一的事情,就是當你試圖加載第二個數據庫:

$DB2=$this->load->database($config); 

當你想要檢索數據庫對象,您必須在第二個參數中通過TRUE

Codeigniter User Guide

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

所以,你的代碼應該改爲:

$DB2=$this->load->database($config, TRUE); 

這將使它發揮作用。

+1

我試過了,但是沒有工作:( –

8

使用此。

$dsn1 = 'mysql://user:[email protected]/db1'; 
$this->db1 = $this->load->database($dsn1, true);  

$dsn2 = 'mysql://user:[email protected]/db2'; 
$this->db2= $this->load->database($dsn2, true);  

$dsn3 = 'mysql://user:[email protected]/db3'; 
$this->db3= $this->load->database($dsn3, true); 

使用

$this->db1 ->insert('tablename', $insert_array); 
$this->db2->insert('tablename', $insert_array); 
$this->db3->insert('tablename', $insert_array); 
+0

您好先生,請您解釋一下我們給出用戶名,密碼,數據庫名稱的簡單例子,我們可以給這種連接在模型構造中。 – whoami

+0

user = username,password = password,db1 = databasename,localhost = hostname – Rayiez

9

最好的方法是使用不同的數據庫羣。如果您想像往常一樣繼續使用master數據庫($ this-> db),只需將持續連接配置選項關閉到輔助數據庫。只有主數據庫應具有持久的Connexion工作:

Master數據庫

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "root"; 
$db['default']['password'] = ""; 
$db['default']['database'] = "database_name"; 
$db['default']['dbdriver'] = "mysql"; 
$db['default']['dbprefix'] = ""; 
$db['default']['pconnect'] = TRUE; 
$db['default']['db_debug'] = FALSE; 
$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; 

輔助數據庫(通知pconnect設置爲false)

$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'] = FALSE; 
$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; 

然後,你可以爲數據庫對象使用輔助數據庫同時使用主數據庫:

// use master dataabse 
$users = $this->db->get('users'); 

// connect to secondary database 
$otherdb = $this->load->database('otherdb', TRUE); 
$stuff = $otherdb->get('struff'); 
$otherdb->insert_batch('users', $users->result_array()); 

// keep using master database as usual, for example insert stuff from other database 
$this->db->insert_batch('stuff', $stuff->result_array()); 
+0

我寫了一篇關於在CodeIgniter應用程序中創建多個數據庫連接的文章。請看看並提出建議https://www.cloudways.com/blog/connect-multiple-databases-codeigniter/ –