CodeIgniter Forums
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 - 08-23-2010

[eluser]Johnathan1707[/eluser]
Hey, I'm going to use CodeIgniter on a web app I'm building and was wondering what the best way to do this would be.
I have one database used for the app itself, members details etc.
The user will enter login credentials for their own databases then they can run queries on it from my site. I was wondering if there is a built in way in CodeIgniter to do this and if not is there a way I can modify the database functions to pass in a db connection.
If I were doing it in plain PHP I'd probably do..
Code:
mysql_query('SELECT some,stuff FROM table',$user_connection);
I'd like something to work like that in my app.

Thank you Smile


Multiple Database Connections - El Forum - 08-23-2010

[eluser]WanWizard[/eluser]
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'] = '...';
// and so on

You can then connect to it using
Code:
// this will make the connection globally available
$CI =& get_instance;
$CI->userdb = $this->load->database('userA', TRUE);
$this->userdb =& $CI->userdb;

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();
$db['hostname'] = '...';
// and so on

// connect to the database
$CI =& get_instance;
$CI->userdb = $this->load->database($db, TRUE);
$this->userdb =& $CI->userdb;



Multiple Database Connections - El Forum - 08-23-2010

[eluser]Johnathan1707[/eluser]
Quick reply Smile

Thanks for that, will have a play trying to get that working tonight. Just one thing, when I'm loading the users databases will I still be able to use my own alongside theirs without passing the user in? So it used mine by default.
Sorry if I'm asking stupid questions, first time working with CodeIgniter.


Multiple Database Connections - El Forum - 08-23-2010

[eluser]WanWizard[/eluser]
You can connect to as many databases as you want.

$this->load->database() will load the database you have defined in your config using the parameter 'active_group'. The others you have to load manually using the method I've described.

So you can do:
Code:
// query your app database
$query1 = $this->db->get('table');

// query your user database
$query2 = $this->userdb->get('table');

There is no such thing as a stupid question. There are only stupid answers. We were first time users once as well... Smile


Multiple Database Connections - El Forum - 09-09-2010

[eluser]kimo_gusatava[/eluser]
how about multiple connection with multiple driver?

can I set $db['userA']['driver'] = 'oci8' or something?

cheers!


Multiple Database Connections - El Forum - 01-19-2011

[eluser]Allan Collins[/eluser]
Forgive me as I'm a noob to CI.

Let's take the above example and say that each user has their own database.

Now, let's say that users and databases are going to be constantly added. Is there a way to dynamically load a specific database?

Example: user table has id, username,password,dbname

Is it possible to load a database object based on the user's dbname field?


Multiple Database Connections - El Forum - 01-19-2011

[eluser]crazypolecat[/eluser]
You can do this dynamically in CodeIgniter. All you need to know is what the name is of the database you wish to connect to. Let's assume that your database name is stored in a variable called $user_db. Connecting to that database could look like this using CodeIgniter.
Code:
$config_db['hostname'] = "localhost";
$config_db['username'] = $username;
$config_db['password'] = $password;
$config_db['database'] = $user_db;
$config_db['dbdriver'] = "mysql";
$config_db['dbprefix'] = "";
$config_db['pconnect'] = TRUE;
$config_db['db_debug'] = TRUE;
$config_db['cache_on'] = FALSE;
$config_db['cachedir'] = "";
$config_db['char_set'] = "utf8";
$config_db['dbcollat'] = "utf8_general_ci";
                
$this->load->database($config_db);

There are other parameters you can pass in the latter load function, which you can find in the user guide (http://ellislab.com/codeigniter/user-guide/database/connecting.html).

I hope this helps.


Multiple Database Connections - El Forum - 01-19-2011

[eluser]Allan Collins[/eluser]
Awesome! That is exactly what I need. Thanks.


Multiple Database Connections - El Forum - 01-19-2011

[eluser]crazypolecat[/eluser]
I just noticed I made a typo in the last line of code. It should be $this->load->database($config_db) instead of $this->load->library($config_db).


Multiple Database Connections - El Forum - 05-01-2012

[eluser]Imran[/eluser]
Hi guys,
I am working on fairly a big size project and using CI for development, everything is going good so far and I am done halfway, now client wants to have one more database and certain part of project will pull the data from this second database.

How can i achieve this by doing minimum changes to whatever is done so far. There will be only few screens which will communicate with second database.
How can I achieve this ?

Will kind reply is highly appreciated.

Thanks