Database.php
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'main_db',
'dbdriver' => 'mysqli',
'dbprefix' => 'sim_',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE,
);
$db['master'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'secondary_db',
'dbdriver' => 'mysqli',
'dbprefix' => 'gap_',
'pconnect' => FALSE,
'db_debug' => FALSE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => FALSE,
);
I'm using two databases in Codeigniter 3 and Its working fine. Now I'm facing a problem of JOINing tables across the above databases. Can it be possible or not? If Yes then how to do that?
Example Code:
public function __construct()
{
parent::__construct();
$this->db_ecom = $this->load->database('master', TRUE);
}
public function getOrderDetails() {
...
$this->db->join('countries', 'countries.country_id=orders.order_country', 'left');
$q = $this->db_ecom->get('orders');
...
}
In the above code example, $this->db
represents main_db
so that $this->db_ecom
represents secondary_db
.
main_db
- contains countries table (country_id, country_name).
secondary_db
- contains orders table (with column Country_id).
So Now I want to JOIN this tables to display the Country as Name not as ID.