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

[eluser]Katie1348[/eluser]
[quote author="jonkenedi" date="1354330680"][quote author="WanWizard" date="1282560730"]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;
[/quote]
Hay WanWizard...
I have try but i get error Using $this when not in object context and...
Code:
// I change $this = $CI... this my config
$CI =& get_instance;
$CI->userdb = $CI->load->database($db, TRUE);
$CI->userdb =& $CI->userdb;
i get error Fatal error: Allowed memory size of... [/quote]

Incase it helps anyone else coming to this topic, there is a typo in the code.

It should read:
Code:
// I change $this = $CI... this my config
$CI =& get_instance();
$CI->userdb = $CI->load->database($db, TRUE);
$CI->userdb =& $CI->userdb;

The change is that get_instance is a method and needs the ().

K


Messages In This Thread
Multiple Database Connections - by El Forum - 08-23-2010, 03:44 AM
Multiple Database Connections - by El Forum - 08-23-2010, 03:52 AM
Multiple Database Connections - by El Forum - 08-23-2010, 03:58 AM
Multiple Database Connections - by El Forum - 08-23-2010, 04:17 AM
Multiple Database Connections - by El Forum - 09-09-2010, 06:50 PM
Multiple Database Connections - by El Forum - 01-19-2011, 05:27 PM
Multiple Database Connections - by El Forum - 01-19-2011, 06:49 PM
Multiple Database Connections - by El Forum - 01-19-2011, 07:35 PM
Multiple Database Connections - by El Forum - 01-19-2011, 07:51 PM
Multiple Database Connections - by El Forum - 05-01-2012, 04:07 AM
Multiple Database Connections - by El Forum - 05-01-2012, 04:26 AM
Multiple Database Connections - by El Forum - 11-30-2012, 07:58 PM
Multiple Database Connections - by El Forum - 05-12-2013, 03:20 AM
Multiple Database Connections - by El Forum - 08-09-2013, 10:58 AM
Multiple Database Connections - by El Forum - 08-09-2013, 11:23 PM
Multiple Database Connections - by El Forum - 08-10-2013, 12:10 AM
Multiple Database Connections - by El Forum - 08-21-2013, 05:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB