在连接查询

时间:2017-02-21 05:43:42

标签: php mysql codeigniter join

我想知道在CodeIgniter中连接查询中连接两个数据库的语法。 我没有办法加入两个数据库。 如果有人有想法,请分享。

1 个答案:

答案 0 :(得分:0)

首先使用project / config / database.php文件添加第二个数据库设置

$db['default'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'db1',
    '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['defaultdb'] = array(
'dsn'   => '',
'hostname' => 'localhost',
'username' => 'root',
'password' => '',
    'database' => 'dcz',
'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);

然后添加代码的代码

    $db1 = $this->load->database('defaultdb',TRUE);

    $queryfromDB1 = $this->db->get('Table_name')->result();

    $queryfromDB2 = $db1->get('Table_name')->result();