CodeIgniter:Multiple Databases

编程入门 行业动态 更新时间:2024-10-28 17:18:01
本文介绍了CodeIgniter:Multiple Databases - 访问第二个数据库中的数据库配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在研究使用多个数据库与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

本文发布于:2023-10-22 20:05:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CodeIgniter   Multiple   Databases

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!