CodeIgniter:多个数据库 - 在第二个数据库中访问数据库配置

时间:2013-03-11 20:55:32

标签: database codeigniter multiple-databases

我一直在寻找使用CodeIgniter的多个数据库。如果我提前知道数据库是什么,那么我可以在配置文件中设置信息,然后调用我需要的任何数据库组。

但是,在我的情况下,我需要将该数据库信息存储在另一个数据库中。它是一种主数据库,包含有关客户的一般信息,包括存储客户数据的数据库和凭据。然后,该供应商可以随时添加客户,并将每个客户的数据分隔在不同的数据库中。

如何根据我在CodeIgniter中从master数据库返回的值设置数据库和凭据,还是有办法做到这一点?

有人能指出我正确的方向吗?提前感谢任何建议。

4 个答案:

答案 0 :(得分:10)

来自文档(https://www.codeigniter.com/user_guide/database/connecting.html):

  

此函数的第一个参数可以选择用于指定   您的配置文件中的特定数据库组,您甚至可以   提交未在您的数据库中指定的数据库的连接值   配置文件

所以你会做这样的事情,用数据库中的值替换值:

$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$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";

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

如果您需要保持与数据库和客户数据库的连接,请将最后一行更改为:

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

// to use the master database:
$this->db->query("SELECT * FROM my_table");

// to then use the customer database:
$customer_db->query("SELECT * FROM whatever");

答案 1 :(得分:7)

使master成为默认数据库,使第二个数据库的客户成为         $ active_group ='default';         $ active_record = TRUE;

    $db['default']['hostname'] = '';
    $db['default']['username'] = '';
    $db['default']['password'] = '';
    $db['default']['dbdriver'] = '';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = TRUE;
    $db['default']['db_debug'] = TRUE;
    $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['secondDatabase']['hostname'] = '';
    $db['secondDatabase']['username'] = '';
    $db['secondDatabase']['password'] = '';
    $db['secondDatabase']['dbdriver'] = '';
    $db['secondDatabase']['dbprefix'] = '';
    $db['secondDatabase']['pconnect'] = TRUE;
    $db['secondDatabase']['db_debug'] = TRUE;
    $db['secondDatabase']['cache_on'] = FALSE;
    $db['secondDatabase']['cachedir'] = '';
    $db['secondDatabase']['char_set'] = 'utf8';
    $db['secondDatabase']['dbcollat'] = 'utf8_general_ci';
    $db['secondDatabase']['swap_pre'] = '';
    $db['secondDatabase']['autoinit'] = TRUE;
    $db['secondDatabase']['stricton'] = FALSE;

您可以通过

在控制器或模型中加载第二个数据库
$DB2 = $this->load->database('secondDatabase', TRUE); 

答案 2 :(得分:3)

/ ** config / database.php ** /

$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = '';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['dbdriver'] = '';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = (ENVIRONMENT !== 'production');
$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; 

/ **您的控制器或型号** /

//by default the master database will be loaded and you can directly access db using      $this->db 
   $result = $this->db->query("SELECT * FROM `your_table`")->limit(1)->get()->result();



$config['dbxyz']['hostname'] = $result->hostname;
$config['dbxyz']['username'] = $result->username;
$config['dbxyz']['password'] = $result->password;
$config['dbxyz']['dbdriver'] = '';
$config['dbxyz']['dbprefix'] = '';
$config['dbxyz']['pconnect'] = TRUE;
$config['dbxyz']['db_debug'] = (ENVIRONMENT !== 'production');
$config['dbxyz']['cache_on'] = FALSE;
$config['dbxyz']['cachedir'] = '';
$config['dbxyz']['char_set'] = 'utf8';
$config['dbxyz']['dbcollat'] = 'utf8_general_ci';
$config['dbxyz']['swap_pre'] = '';
$config['dbxyz']['autoinit'] = TRUE;
$config['dbxyz']['stricton'] = FALSE;

//load database config
$this->config->load('database');

//Set database config dynamically        
$this->config->set_item('dbxyz', $config);

//Now you can load the new database using
$this->dbxyz = $this->load->database('dbxyz'); 

注意:有关详细信息,请参阅Config Class Codeigniter documentation

答案 3 :(得分:1)

在application \ config \ database.php

中添加以下行
$sql = '';
$text = file_get_contents('keywords.txt');
foreach (explode($text, PHP_EOL) as $keyword)
    $sql .= "INSERT INTO tmpText (keyword) VALUES ('$keyword'); ";
if (mysqli_multi_query($conn, $sql)) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
// Now run this query:
//   SELECT keyword, COUNT(description) AS Matches 
//   FROM tmpText, items 
//   WHERE  description LIKE CONCAT('%',keyword,'%')
//   GROUP BY keyword

现在我们在Controller和model中使用我们的第二个数据库,如下所示。

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

我参考了http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php。它为我工作。