0

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

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

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

这是我的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;

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

4

2 回答 2

1

我的整个数据库设置示例

// 我的数据库.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');
于 2013-03-01T03:38:41.857 回答
0

尝试在您的配置 database.php 中更改它

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

$db['default']['pconnect'] = FALSE;
于 2013-02-28T12:40:26.833 回答