如何在codeigniter中连接两个以上的数据库

时间:2016-10-18 10:21:47

标签: php sql database codeigniter sql-server-2008

我想连接3个数据库。如果我只连接两个数据库,我没有任何问题。当我尝试连接3个数据库时。我有问题,就像我无法连接到数据库二。

这是我在database.php的配置:

$db['default'] = array(
'dsn'   => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432@',
'database' => 'HSP',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['crm'] = array(
'dsn'   => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432@',
'database' => 'CRM',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

$db['pltapol'] = array(
'dsn'   => '',
'hostname' => '192.168.11.29,1433',
'username' => 'userhsp',
'password' => 'hsp432@',
'database' => 'pltapol',
'dbdriver' => 'mssql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => FALSE,
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);

在我的模型中这样:

private $db2;
private $db3;

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

我只能从数据库 pltapol 获取对象,但我不会从 crm 获取对象。如何解决?

1 个答案:

答案 0 :(得分:0)

我想回答我的问题。我试着用

$this->db2->db_select() or $this->db3->db_select() 

如果我想在$this->db2->query() or before $this->db2->query()

之前使用该连接

这是工作!

快乐的编码:)