Codeigniter - 连接到不同服务器上的多个数据库

时间:2016-10-22 09:54:27

标签: database codeigniter

我想实现一个功能,我想根据用户选择的公司更改数据库。

例如:
User A访问我的网站时,他需要登录,登录后他可以搜索他的公司,直到这一点我将用户连接到我的服务器数据库;在他搜索他的公司并按下完成按钮后,我必须连接到他的服务器并动态地将我的服务器数据库更改为他的公司服务器数据库。 (这样做的原因是我的网络应用程序将由多家公司使用,因此公司将拥有不同的员工记录,所以我必须连接到他们的服务器的数据库以获得他们的雇主记录。)

我真的很困惑如何实现这个我需要存储在我的数据库中的数据,以便连接到不同的服务器数据库? 到目前为止我研究过,我知道我需要存储dbname,dbusername,dbPassword

我还需要存储其他东西吗?我是否还需要存储主机名?您能否指导我如何实现这一目标?

1 个答案:

答案 0 :(得分:0)

Are you creating individual database as per the company name of that employee, or are you using same database for all the employees?

If you are creating individual database for individual users, you store the company name in to your users information table and when ever you are login, you fetch the company name of that user and store it in to session

and while fetching the record you use the stored company name in to the session for connecting to the database as :

suppose you want to fetch the record from companymaster or some other table you can use like as :

 $companyName=$this->session->userdata('companyName');//stored company name in to the session
Now, use the table as

$this->db->from("$companyName"."Your Table Name");

Thanks.