CodeIgniter Forums
Setting up 2 databases - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Setting up 2 databases (/showthread.php?tid=64584)



Setting up 2 databases - doomie22 - 03-07-2016

Hi there, 

I am just trying to setup another database within my project. I have done the following within database.php

PHP Code:
$active_group 'default';
$query_builder TRUE;

$db['default'] = array(
    <
details of standard database in here>
);

$db2['default'] = array(
    <
details of 2nd database in here>
); 

I have taken out the details so that its not massive.  I have tried to connect to it but it just errors with cannot find $db2 and I am getting no where.

Can anyone show me how I can connect to this second database so I can get information from it.

Thanks,

Doomie


RE: Setting up 2 databases - donpwinston - 03-07-2016

http://codeigniter.com/user_guide/database/connecting.html


RE: Setting up 2 databases - mixinix - 03-07-2016

(03-07-2016, 12:19 PM)Your config: Wrote:
PHP Code:
$active_group 'default';
$query_builder TRUE;

$db['db1_name'] = array(
    <
details of standard database in here>
);

$db['db2_name'] = array(
    <
details of 2nd database in here>
); 

In your models...

PHP Code:
$db1 $this->load->database('db1_name'true);
$db2 $this->load->database('db2_name'true); 

Access with:
PHP Code:
$q1 $db1->query(...)->... or with other methods
$q2 
$db2->query()... etc