0

I want to implement a functionality where I want to change the database based on the user selected company.

For example:
When User A visit my site he needs to login, after the login he can search his company, until this point I am connecting the user to my server db; after he search his company and press the done button I have to make a connection to his server and dynamically change my server db to his company server db. (the reason of doing this is my web app is gonna used by multiple companies so companies gonna have different employee records so i have to connect to db of their server to get their employers records).

I am really confused how to achieve this what data I need to store in my db for making connection to different server db?
As far I researched I understood that I need to store the dbname,dbusername,dbPassword.

Do i need to store anything else? Do I need to store the host name too? Can any of you guide me how to achieve this?

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
Tarzy
  • 107
  • 1
  • 10
  • Welcome to Stack Overflow, Tarzy, your question might be good, but it is a huge chunk of text without code snippets or images. – Sainan Oct 22 '16 at 09:56
  • Possible duplicate: [Codeigniter - multiple database connections](http://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections) – oguzhancerit Oct 22 '16 at 09:59
  • i know but sorry to explain my question i can just used some text..I am asking for an approach to solve my problem – Tarzy Oct 22 '16 at 10:01

1 Answers1

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.