我一直在寻找到使用多个数据库使用CodeIgniter。 如果我知道这个数据库是时间提前,那么我可以在配置文件中设置的信息,然后调用我需要哪个数据库组。
在我的情况,但是,我需要存储在另一个数据库的数据库信息。 它是那种一个主数据库有关,包括数据库和证书,客户的数据存储在一个客户的一般信息。然后,这个供应商可以增加客户每次想和在不同的数据库中每个客户的数据隔离。
如何设置基于我从CI中的主数据库返回的值的数据库和凭据,或者是有甚至办法做到这一点?
任何人都可以点我在正确的方向? 在此先感谢您的任何建议。
从文档( 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");
使主默认数据库和客户的第二个数据库
$ active_group = '默认'; $ 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);
/ **配置/ 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');
注:欲了解更多详情,请参阅配置类笨文档
下面添加应用程序\设置\ 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;
现在我们用我们的第二个数据库在我们的控制器和模型如下图所示。
$CI = &get_instance();
$this->db2 = $CI->load->database('mydb2', TRUE);
$qry = $this->db2->query("SELECT * FROM employee");
print_r($qry->result());
我从取出的参考http://www.tutsway.com/use-multiple-db-connections-in-codeigniter.php 。它的工作对我来说。
文章来源: CodeIgniter: Multiple Databases - Accessing database config in a second database