CodeIgniter Forums

Full Version: one codeigniter application folder run two databases
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I know that it's possible to use a different database on the same connection but i want to use differents databases with differents connections with my CI application.

I have an application for many customers (so one codeigniter application folder), every customer has his own database and his own domain name but all clients need only one application. 

So I want to choose the database according to the domain :
  • domain.com/customer1/ ==> choose connection with database1
  • domain.com/customer2/ ==> choose connection with database2
Thanks for helping me with a solution.
@morsi,

Here is how you do multiple connections to multiple databases: https://www.codeigniter.com/user_guide/d...-databases

Then all you have to do is when you set your $DB at the top of your model use the ternary option like this
$DB = (site_url() == 'domain1')? $this->load->database('db_connection1', TRUE): $this->load->database('db_connection2', TRUE);
(05-10-2019, 06:51 AM)php_rocs Wrote: [ -> ]@morsi,

Here is how you do multiple connections to multiple databases: https://www.codeigniter.com/user_guide/d...-databases

Then all you have to do is when you set your $DB at the top of your model use the ternary option like this
$DB = (site_url() == 'domain1')? $this->load->database('db_connection1', TRUE): $this->load->database('db_connection2', TRUE);

Thanks for help but i think i must also change $config['base_url'] into config.php ?

How i can do it ? (Remmembre, i want to use one CI folder and files with many domains and databases)

Thanks.