2011-11-25 116 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/ –