Codeigniter:我们如何连接两个数据库。如果是这样,我们如何同时使用它们

时间:2015-08-12 13:49:11

标签: php codeigniter

我在CodeIgniter中使用此代码添加数据库:

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

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

如何添加第二个数据库? 我怎样才能同时使用它们? 任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:-1)

来自文档:

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

在你的模型中你可以写:

function method()
{
  $test = $this->load->database('test', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

  $query = $test->select('first_name, last_name')->get('person');
  var_dump($query);
}

阅读更多(http://www.codeigniter.com/user_guide/database/configuration.html