Database Config Load Question |
[eluser]ChrisMiller[/eluser]
Ok basically I am sure I am just skipping over it somewhere but what I want to do is use the ci database library twice in a controller, when the script is loaded it connects to the database in the config file which is easy to do but then I want to connect to a seperate database using a config specified in file... so the result is $this->db-> = the normal database and $this->app_db = the new loaded database....? any ideas? also I cant just put the config values in the database config file as they are loaded from a database depending on what user is accessing the app. Code: $config['hostname'] = "localhost";
[eluser]Craig A Rodway[/eluser]
Firstly, the DB config file should be set out in this format with both of your database connection details in, with the first config array key being the name: Code: $db['db1']['hostname'] = "localhost"; If you automatically load db1 ($active_group, as explained in Databae Configuration) then you can connect to db2 like this: Code: // Connect to db2 And still access db1 as normal: Code: $this->db->query('SELECT * FROM table1'); User Guide - Connecting to your database.
[eluser]ChrisMiller[/eluser]
Thanks I already read all of that the problem is that I cannot save the second database configuration to the file because they are supplied via the first database that is loaded. The way it works is I am writing a master controller that can manage multiple databases depending on what each user has access to. So simply put: A. Default Database is Loaded B. User Account Info is Loaded C. Second Database is loaded via info supplied in part B. D. Fetch info from second database.
[eluser]Craig A Rodway[/eluser]
Ah, okay. A mixture of the two should work then: Code: // Get the DB info we need from first DB as set in config/database.php
[eluser]ZeusChicago[/eluser]
Have you tested the above and confirmed that it worked in 1.6? I tried your first suggestion with the array of database configurations but when I do this Code: $db['db1']['hostname'] = "localhost"; I get a Call to member function Query() on non-object and if I try to do a print_r() on the $DB2 object it returns null When I attempt to use your second suggestion with Code: $sql = 'SELECT * FROM slave_table LIMIT 1'; Thanks in advance! Z
[eluser]alxjvr[/eluser]
[quote author="ZeusChicago" date="1205046126"] // Connect to db2 $DB2 = $this->load->database('slave', TRUE); // Query on db2 $query = $DB2->query('SELECT * FROM table'); [/code] [/quote] In this line: $DB2 = $this->load->database(’slave’, TRUE); I think you need to change 'slave' to 'db2' since you defined the second db as $db['db2']... etc. |
Welcome Guest, Not a member yet? Register Sign In |