CI与不同服务器的多个数据库连接

时间:2013-02-28 08:24:51

标签: php codeigniter phpmyadmin

我有一个应用程序需要连接到2个不同的数据库和不同的服务器。除了在database.php中执行此操作之外,还有其他任何设置吗?我在我的代码中写了这个连接到db:

$provinsi_db = $this->load->database('provinsi', true); //this is from another server
$local = $this->load->database('default', true); //this one is in my localhost

但是当我试图从服务器db中选择数据时,没有发生任何事情..虽然从本地数据库中选择数据没有问题..任何人都可以帮助我吗?

这是我的database.php:

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

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username_local';
$db['default']['password'] = 'password_local';
$db['default']['database'] = 'db_local';
$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['provinsi']['hostname'] = 'xxx.xxx.xxx.xxx';
$db['provinsi']['username'] = 'username_foreign';
$db['provinsi']['password'] = 'password_foreign';
$db['provinsi']['database'] = 'db_foreign';
$db['provinsi']['dbdriver'] = 'mysql';
$db['provinsi']['dbprefix'] = '';
$db['provinsi']['pconnect'] = TRUE;
$db['provinsi']['db_debug'] = TRUE;
$db['provinsi']['cache_on'] = FALSE;
$db['provinsi']['cachedir'] = '';
$db['provinsi']['char_set'] = 'utf8';
$db['provinsi']['dbcollat'] = 'utf8_general_ci';
$db['provinsi']['swap_pre'] = '';
$db['provinsi']['autoinit'] = TRUE;
$db['provinsi']['stricton'] = FALSE;

我是否必须在服务器的“主机名”中包含端口?

2 个答案:

答案 0 :(得分:1)

我的整个数据库设置示例

//我的database.php

/* API Database Connection */

$active_group = 'apidb';
$active_record = TRUE;

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


/* Site Database Connection */

$active_group = 'sitedb';
$active_record = TRUE;

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

在控制器中,加载数据库

$this->sitedb = $this->load->database('sitedb', TRUE);
$this->apidb = $this->load->database('apidb', TRUE);

在模型中,您可以调用

$this->apidb->query('your query');

$this->sitedb->query('your query');

答案 1 :(得分:0)

尝试在config database.php

中更改此设置
$db['provinsi']['pconnect'] = FALSE;

$db['default']['pconnect'] = FALSE;