CodeIgniter Forums
one codeigniter application folder run two databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: one codeigniter application folder run two databases (/thread-73573.html)



one codeigniter application folder run two databases - morsi - 05-10-2019

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.


RE: one codeigniter application folder run two databases - php_rocs - 05-10-2019

@morsi,

Here is how you do multiple connections to multiple databases: https://www.codeigniter.com/user_guide/database/connecting.html#connecting-to-multiple-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);


RE: one codeigniter application folder run two databases - morsi - 05-10-2019

(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/database/connecting.html#connecting-to-multiple-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.


RE: one codeigniter application folder run two databases - InsiteFX - 05-10-2019

Setting your base url in Codeigniter