Welcome Guest, Not a member yet? Register   Sign In
Database Config Load Question
#1

[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";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['active_r'] = TRUE;

$this->load->database($config);
How can I get that so it can be loaded via a diffrent class name like I can do with my libraries. ie $this->db2->
#2

[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";
$db['db1']['username'] = "root";
$db['db1']['password'] = "";
$db['db1']['database'] = "database_name_one";
$db['db1']['dbdriver'] = "mysql";
$db['db1']['dbprefix'] = "";
$db['db1']['pconnect'] = TRUE;
$db['db1']['db_debug'] = FALSE;
$db['db1']['active_r'] = TRUE;

$db['db2']['hostname'] = "localhost";
$db['db2']['username'] = "root";
$db['db2']['password'] = "";
$db['db2']['database'] = "database_name_two";
$db['db2']['dbdriver'] = "mysql";
$db['db2']['dbprefix'] = "";
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = FALSE;
$db['db2']['active_r'] = TRUE;

If you automatically load db1 ($active_group, as explained in Databae Configuration) then you can connect to db2 like this:

Code:
// Connect to db2
$DB2 = $this->load->database('db2', TRUE);
// Query on db2
$query = $DB2->query('SELECT * FROM table');

And still access db1 as normal:

Code:
$this->db->query('SELECT * FROM table1');


User Guide - Connecting to your database.
#3

[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.
#4

[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
$sql = 'SELECT * FROM table LIMIT 1';
$query = $this->db->query($sql);
$row = $query->row();

// Create new database configuration array using info from previous SQL query
$config['hostname'] = $row->hostname;
$config['username'] = $row->username;
$config['password'] = $row->password;
$config['database'] = $row->database;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['active_r'] = TRUE;

// Connect to second database with values from above and assign to new variable
$DB2 = $this->load->database($config, TRUE);
#5

[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";
$db['db1']['username'] = "master";
$db['db1']['password'] = "password";
$db['db1']['database'] = "master";
$db['db1']['dbdriver'] = "mysqli";
$db['db1']['dbprefix'] = "";
$db['db1']['pconnect'] = TRUE;
$db['db1']['db_debug'] = FALSE;
$db['db1']['active_r'] = TRUE;

$db['db2']['hostname'] = "localhost";
$db['db2']['username'] = "slave";
$db['db2']['password'] = "password";
$db['db2']['database'] = "slave";
$db['db2']['dbdriver'] = "mysqli";
$db['db2']['dbprefix'] = "";
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = FALSE;
$db['db2']['active_r'] = TRUE;

// Connect to db2
$DB2 = $this->load->database('slave', TRUE);
// Query on db2
$query = $DB2->query('SELECT * FROM table');

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';
$query = $this->db->query($sql);
$row = $query->row();

// Create new database configuration array using info from previous SQL query
$config['hostname'] = $row->hostname;
$config['username'] = $row->username;
$config['password'] = $row->password;
$config['database'] = $row->database;
$config['dbdriver'] = "mysqli";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['active_r'] = TRUE;

// Connect to second database with values from above and assign to new variable
$DB2 = $this->load->database($config, TRUE);
The model I am trying to load still defaults back to the default db, instead of using the alternate database I selected

Thanks in advance!

Z
#6

[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.




Theme © iAndrew 2016 - Forum software by © MyBB