Multiple Database Connections - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Multiple Database Connections (/showthread.php?tid=33331) Pages:
1
2
|
Multiple Database Connections - El Forum - 05-01-2012 [eluser]InsiteFX[/eluser] CodeIgniter Users Guide - Connecting to your Database Multiple Database Connections - El Forum - 11-30-2012 [eluser]Unknown[/eluser] [quote author="WanWizard" date="1282560730"]You don't need to resort to manual coding, just use the database library to connect to multiple databases. In your config/database.php, create a default entry for your 'app' database. You can connect to that database using Code: $this->load->database(); And access it using Code: $this->db->... For your user databases, you could simply add a section to config/database.php, assuming that you have some process in place to create these databases for these users: Code: $db['userA']['hostname'] = '...'; You can then connect to it using Code: // this will make the connection globally available If you can't/won't have these userdb configurations in the config file, you can also generate the config array at runtime: Code: $db = array(); Hay WanWizard... I have try but i get error Using $this when not in object context and... Code: // I change $this = $CI... this my config Multiple Database Connections - El Forum - 05-12-2013 [eluser]Katie1348[/eluser] [quote author="jonkenedi" date="1354330680"][quote author="WanWizard" date="1282560730"]You don't need to resort to manual coding, just use the database library to connect to multiple databases. In your config/database.php, create a default entry for your 'app' database. You can connect to that database using Code: $this->load->database(); And access it using Code: $this->db->... For your user databases, you could simply add a section to config/database.php, assuming that you have some process in place to create these databases for these users: Code: $db['userA']['hostname'] = '...'; You can then connect to it using Code: // this will make the connection globally available If you can't/won't have these userdb configurations in the config file, you can also generate the config array at runtime: Code: $db = array(); Hay WanWizard... I have try but i get error Using $this when not in object context and... Code: // I change $this = $CI... this my config Incase it helps anyone else coming to this topic, there is a typo in the code. It should read: Code: // I change $this = $CI... this my config The change is that get_instance is a method and needs the (). K Multiple Database Connections - El Forum - 08-09-2013 [eluser]darjap[/eluser] Hi! I tried to use this code, since i have to do a similar thing with my application, but i have a problem - when i use the database connection outside the module i have created it, it doesnt work - error: Severity: Notice Message: Undefined variable: CI I would be really thankful for any help! Code: public function connect_company() Simple usage of the database from another/same module - it works when it is in the same module, does not, when it is in another: Code: $query = $this->company_db->get('clients'); Multiple Database Connections - El Forum - 08-09-2013 [eluser]Katie1348[/eluser] Hi Darjap, What file and folder are you storing this in? How are you accessing the method/function? Please be specific. I ask, because if it is a helper, then this will not work as helpers are not part of the class system, but are separate functions. K Multiple Database Connections - El Forum - 08-10-2013 [eluser]darjap[/eluser] both of them are stored within modules/sth/models/file.php ...it works fine if i store the conection code in config/database.php, but i need a dynamic login into mysql. The function is accessed the regular way: Code: $this->load->model('mdl_sessions'); Again, if i use it within the same module(in the "do stuff" section), everything works as it should, but if i redirect to another module and try to do something else with that database, it just gives the error of database not being chosen. Multiple Database Connections - El Forum - 08-21-2013 [eluser]Katie1348[/eluser] [quote author="darjap" date="1376071100"]Hi! I tried to use this code, since i have to do a similar thing with my application, but i have a problem - when i use the database connection outside the module i have created it, it doesnt work - error: Severity: Notice Message: Undefined variable: CI I would be really thankful for any help! Code: public function connect_company() Simple usage of the database from another/same module - it works when it is in the same module, does not, when it is in another: Code: $query = $this->company_db->get('clients'); [/quote] I think you might have an error in your code. Code: $CI->company_db =& $CI->company_db; I think it should read Code: $this->company_db =& $CI->company_db; That should as far as I can see fix your problem and let you use the company_db method of accessing the database connection etc. Good luck. kate |