Welcome Guest, Not a member yet? Register   Sign In
Multiple Database Connections

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..
mysql_query('SELECT some,stuff FROM table',$user_connection);
I'd like something to work like that in my app.

Thank you Smile

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

And access it using

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

You can then connect to it using
// 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:
$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;

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.

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:
// 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

how about multiple connection with multiple driver?

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


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

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.
$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";

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-gui...cting.html).

I hope this helps.

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

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

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.


Theme © iAndrew 2016 - Forum software by © MyBB