我一直在研究使用多个数据库与CodeIgniter。如果我知道数据库是提前的,那么我可以在配置文件中设置信息,然后调用我需要的数据库组。
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.
然而,在我的情况下,我需要将数据库信息存储在另一个数据库中。它是一种具有关于客户的一般信息的主数据库,包括客户的数据存储在其中的数据库和凭证。然后该供应商可以在他们想要时添加客户,并且将每个客户的数据分离在不同的数据库中。
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.
如何根据我在CodeIgniter中从主数据库获取的值设置数据库和凭据,或者是否有办法呢?
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?
任何人都能指向正确的方向?感谢您提供任何建议。
Can anyone point me in the right direction? Thanks in advance for any advice.
推荐答案从文档(ellislab/codeigniter/user-guide/database/connecting.html ):
此函数的第一个参数可以用于指定a从配置文件指定特定数据库组,或者甚至可以为数据库提交连接值
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 master database:
$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);如果您需要维护与主数据库 strong> customer 数据库,然后将最后一行更改为:
If you need to maintain a connection to the master database and the customer database, then change the last line to:
$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");更多推荐
CodeIgniter:Multiple Databases
发布评论