The following code is something great that I have learned today. You can access more than one database with the CodeIgniter php framework without editing to much lines. Consider the code below: Within the application/config/database.php config file you can add more that one set of database parameters.

//database parameter set 1
$db['01']['hostname'] = 'localhost';
$db['01']['username'] = 'root';
$db['01']['password'] = '';
$db['01']['database'] = 'multi_01';

//database parameter set 2
$db['02']['hostname'] = 'localhost';
$db['02']['username'] = 'root';
$db['02']['password'] = '';
$db['02']['database'] = 'multi_02';



You can then create database variables for each of the databases that you want to access

//calling database parameter set 1
$this->db01 = $this->load->database('01', true);

//calling database parameter set 2
$this->db02 = $this->load->database('02', true);



And then by using those created variables, you can get data from those databases.
* Note: I am using the active records methods *

//getting all users from database db01
$database_1_results = $this->db01->get('users');

//getting categories with the user id 1 from db02
$uid = 1;
$database_2_results = $this->db02->get_where('cats', array('uid'=>$uid));
 

Leave a Reply

Set your Twitter account name in your settings to use the TwitterBar Section.