我们可以在Codeigniter中连接多个数据库吗?

时间:2015-08-28 11:56:05

标签: php mysql codeigniter multiple-databases

我正面临这个问题,无法连接到另一个数据库。

以下是我的代码。我已经定义了两个数据库但是当我尝试获取所有记录时,我得到以下错误:

Table 'crm.cdr_Feb2015' doesn't exist.

即使我已经在控制器中加载了数据库,它也会选择默认数据库来选择详细信息。

$active_group = 'default';
    $active_record = TRUE;

    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = 'root';
    $db['default']['password'] = '*****';
    $db['default']['database'] = 'crm';
    $db['default']['dbdriver'] = 'mysql';
    $db['default']['dbprefix'] = '';
    $db['default']['pconnect'] = FALSE;
    $db['default']['db_debug'] = TRUE;
    $db['default']['cache_on'] = TRUE;
    $db['default']['cachedir'] = '';
    $db['default']['char_set'] = 'utf8';
    $db['default']['dbcollat'] = 'utf8_general_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = TRUE;

    $db['asterisk']['hostname'] = 'localhost';
    $db['asterisk']['username'] = 'root';
    $db['asterisk']['password'] = '*****';
    $db['asterisk']['database'] = 'asterisk';
    $db['asterisk']['dbdriver'] = 'mysql';
    $db['asterisk']['dbprefix'] = '';
    $db['asterisk']['pconnect'] = FALSE;
    $db['asterisk']['db_debug'] = TRUE;
    $db['asterisk']['cache_on'] = TRUE;
    $db['asterisk']['cachedir'] = '';
    $db['asterisk']['char_set'] = 'utf8';
    $db['asterisk']['dbcollat'] = 'utf8_general_ci';
    $db['asterisk']['swap_pre'] = '';
    $db['asterisk']['autoinit'] = TRUE;
    $db['asterisk']['stricton'] = TRUE;

现在在我的控制器中,我已经加载了数据库:

  public function index()
{
    $this->load->database('asterisk', TRUE);

    $test =$this->cdr_m->get();
    var_dump($test);

}

模型文件:

    <?php
     class Cdr_M extends MY_Model
    {

    protected $_table_name = 'cdr_Feb2015';
    protected $_order_by = '';
    protected $_timestamps = TRUE;

    function __construct()
    {
        parent::__construct();
    }
}

3 个答案:

答案 0 :(得分:2)

$this是默认数据库的实例。您需要分配到不同的变量

$this->db2 = $this->load->database('asterisk', TRUE);
$test = $this->db2->cdr_m->get();
var_dump($test);

答案 1 :(得分:2)

配置/ database.php中

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = 'xxxxx';
$db['default']['database'] = 'CloudAgent';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = 'xxxxx';
$db['db2']['database'] = 'Asterisk';
$db['db2']['dbdriver'] = 'mysql';
$db['db2']['dbprefix'] = '';
$db['db2']['pconnect'] = TRUE;
$db['db2']['db_debug'] = TRUE;
$db['db2']['cache_on'] = FALSE;
$db['db2']['cachedir'] = '';
$db['db2']['char_set'] = 'utf8';
$db['db2']['dbcollat'] = 'utf8_general_ci';
$db['db2']['swap_pre'] = '';
$db['db2']['autoinit'] = TRUE;
$db['db2']['stricton'] = FALSE;

控制器:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Analytics extends CI_Controller {


    function __construct()
          {
            parent::__construct();
            $this->load->database();
            $this->db2 = $this->load->database('db2',TRUE); 

          }

}

查询:

$in_calls = $this->db2->query('SELECT DATE_FORMAT(query_time,"%Y-%m-%d %h:%i%p") AS FromHour,total_calls AS callcount FROM LiveCalls WHERE DATE(query_time) = DATE(NOW())'); 

LiveCalls表属于Asterisk数据库。

答案 2 :(得分:2)

试试这个:

 public function index()
{
    $this->db = $this->load->database('asterisk', TRUE);

    $test =$this->cdr_m->get();
    var_dump($test);

}