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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15348423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:53:39  来源:igfitidea点击:

CodeIgniter: Multiple Databases - Accessing database config in a second database

databasecodeignitermultiple-databases

提问by spacemunkee

I've been looking into using multiple databases with CodeIgniter. If I know what the databases are ahead of time, then I can set the information in the config file and then call whichever database group I need.

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

In my situation, however, I need to store that database information in another database. It is sort of a master database with general information about a customer including the database and credentials that the customer's data is stored in. This vendor can then add customers whenever they want and have each customer's data segregated in different databases.

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

How can I set the database and credentials based on the values I get back from the master database in CodeIgniter, or is there even a way to do that?

如何根据我从 CodeIgniter 中的主数据库返回的值设置数据库和凭据,或者甚至有办法做到这一点?

Can anyone point me in the right direction? Thanks in advance for any advice.

任何人都可以指出我正确的方向吗?提前感谢您的任何建议。

回答by swatkins

From the docs ( https://www.codeigniter.com/user_guide/database/connecting.html) :

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

The first parameter of this function can optionally be used to specify a particular database group from your config file, or you can even submit connection values for a database that is not specified in your config file.

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

So you would do something like this, replacing the values with values from the masterdatabase:

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

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

If you need to maintain a connection to the masterdatabase and the customerdatabase, then change the last line to:

如果需要保持master数据库和customer数据库的连接,那么最后一行修改为:

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

回答by jalborres

Make the master a default database and the customer for second database
$active_group = 'default'; $active_record = TRUE;

将主数据库设为默认数据库,将客户设为第二个数据库
$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;

you can load the second database in controller or in model by

您可以通过以下方式在控制器或模型中加载第二个数据库

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

回答by Girish

/** config/database.php **/

/** 配置/数据库.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; 

/** Your controller or model **/

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

//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'); 

NOTE: For more details, refer Config Class Codeigniter documentation

注意:有关更多详细信息,请参阅配置类 Codeigniter 文档

回答by Manish

Add below line in application\config\database.php

在 application\config\database.php 中添加以下行

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

Now we use our second database in our Controller and model like below.

现在我们在控制器和模型中使用我们的第二个数据库,如下所示。

$CI = &get_instance();
$this->db2 = $CI->load->database('mydb2', TRUE);
$qry = $this->db2->query("SELECT * FROM employee");
print_r($qry->result());

I have taken reference from http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php.It's work for me.

我参考了http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php 。它对我有用。