How to join two tables from different databases using codeigniter?

Viewed 60

How to make this query join two tables from different databases?. I already have configured database.php in CodeIgniter with the two databases.

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '******',
    'password' => '******',
    'database' => '******',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

$db['simak'] = array( 
    'dsn'   => '',
    'hostname' => '103.124.44.13',
    'username' => '******',
    'password' => '******',
    'database' => '******',
    'dbdriver' => 'mysqli',
    'port'      => 21,
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

My model

function __construct() {
    $this->db_simak = $this->load->database('simak', true);
}

private function List() {
    $this->db->select('a.*, b.nama_diklat, c.kode_ese');
    $this->db->from('kis_diklats a');
    $this->db_simak->join('md_diklat b', 'a.id_diklat = b.id_diklat', 'left');
    $this->db_simak->join('m_unor c', 'a.kode_ese = c.kode_ese', 'left');
    
    if ($this->session->userdata('level') != 1) {
        $this->db->where('a.dihapus_oleh', NULL);
    }

Error notice

A Database Error Occurred
Error Number: 1054
Unknown column 'b.nama_diklat' in 'field list'
SELECT a.*, b.nama_diklat, c.kode_ese FROM kis_diklats a ORDER BY diinput_tgl DESC LIMIT 10

0 Answers
Related