Welcome Guest, Not a member yet? Register   Sign In
connect to multiple databases
#3

[eluser]Maglok[/eluser]
Yes I have tackled this before myself. You have to define a second set of database parameters. CI isn't developed to really have two DB connections though, it is more for swapping test and production dbases. That said there are some tricks around it. So first define a second set of DB info like so:

Code:
/* FORUM */
$active_group = "forum";
$active_record = TRUE;

$db['forum']['hostname'] = "xxxxx";
$db['forum']['username'] = "xxxxx";
$db['forum']['password'] = "xxxxx";
$db['forum']['database'] = "xxxxx";
$db['forum']['dbdriver'] = "mysql";
$db['forum']['dbprefix'] = "";
$db['forum']['pconnect'] = TRUE;
$db['forum']['db_debug'] = TRUE;
$db['forum']['cache_on'] = FALSE;
$db['forum']['cachedir'] = "";
$db['forum']['char_set'] = "utf8";
$db['forum']['dbcollat'] = "utf8_general_ci";

/* TEST SITE */
$active_group = "default";
$active_record = TRUE;

$db['default']['hostname'] = "xxxxx";
$db['default']['username'] = "xxxxx";
$db['default']['password'] = "xxxxx";
$db['default']['database'] = "xxxxx";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

Your active db will be the one you defined LAST.

Once you have done this you can manually connect to the second one (or put it in MY_Controller if you always need to). You can then load your second database like so:

Code:
$this->db_forum = $this->CI->load->database('forum', TRUE);

Access dbase 1 with $this->db and dbase 2 with $this->db_forum (or whatever you called it).


Messages In This Thread
connect to multiple databases - by El Forum - 02-16-2010, 03:27 AM
connect to multiple databases - by El Forum - 02-16-2010, 03:38 AM
connect to multiple databases - by El Forum - 02-16-2010, 03:40 AM
connect to multiple databases - by El Forum - 02-16-2010, 04:12 AM
connect to multiple databases - by El Forum - 02-24-2010, 08:47 PM
connect to multiple databases - by El Forum - 04-17-2012, 06:00 AM
connect to multiple databases - by El Forum - 07-10-2012, 08:50 AM
connect to multiple databases - by El Forum - 08-02-2012, 03:35 AM
connect to multiple databases - by El Forum - 08-15-2012, 07:45 AM
connect to multiple databases - by El Forum - 12-18-2013, 11:30 PM
connect to multiple databases - by El Forum - 07-03-2014, 06:38 AM



Theme © iAndrew 2016 - Forum software by © MyBB