简体   繁体   English

Codeigniter-多个数据库连接

[英]Codeigniter - multiple database connections

I have a problem with multiple db connections at Codeigniter. 我在Codeigniter遇到多个数据库连接问题。 At my database.php i configured two databases. 在我的database.php中,我配置了两个数据库。

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

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

At my model i use this when i want to connect to a other db than the usual one: 在我的模型中,当我想连接到另一个数据库而不是通常的数据库时,可以使用它:

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

But at all time codeigniter connects to cms. 但是在任何时候codeigniter都连接到cms。 When i remove 当我删除

$active_group = 'cms';
$active_record = FALSE;

Codeingiter gives an error. Codeingiter给出错误。 When i tried this 当我尝试这个

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

function test()
{
      $query  = "SELECT * FROM cms_modules";
      $result = $db2->db->query($query);
      return $db2->result();
}

It gives an error. 它给出了一个错误。 Variabele db2 does not exist. Variabele db2不存在。 I just want to choose, at every model, wich db i want to connect to. 我只想在每个模型中选择要连接的数据库。 But is doesn,t work. 但是是行不通的。 Does somebody know, how i can work with different databases at models. 有人知道吗,我如何在模型上使用不同的数据库。

Thank you very much!! 非常感谢你!!

You have to save the variable $db2 as a class field. 您必须将变量$ db2保存为类字段。 The you can access $this->db2 ... 您可以访问$ this-> db2 ...

为了将来的访问者参考,负载将遵循以下原则:

$this->db2 = $this->load->database('hazeleger',true);

You have to change db2 class 您必须更改db2类

    $query  = "SELECT * FROM cms_modules";
    $result = $this->db2->query($query);
    return result();

This happens because you have most probably set in /application/config/autoload.php that the database library is automatically loaded/created. 发生这种情况是因为您很可能已在/application/config/autoload.php中设置为自动加载/创建数据库库。

Open autoload.php and look for this line: 打开autoload.php并查找以下行:

 $autoload['libraries'] = array('database');

Remove 'database' from the array and save. 从阵列中删除“数据库”并保存。 Now it should be working in your controllers as intended. 现在它应该可以按预期在您的控制器中工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM