2013-02-28 101 views
0

我有一个应用程序需要连接到2个不同的数据库,并在不同的服务器。有没有其他的设置,我不得不在做database.php?我写这在我的代码连接到这两个数据库:CI多个数据库连接不同的服务器

$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; 

我一定要包括在服务器的“主机名”端口?

+0

你设置为'database.php' DB既CONFIGS?你可以告诉我们你的'database.php'吗? – deadlock 2013-02-28 08:26:20

+0

是的,我有,我现在只包括它..我不认为它有任何问题。 – user2118738 2013-03-01 03:30:59

回答

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'); 
+0

它的工作原理!谢谢。我实际上是在PHP和CI新,所以非常感谢:) – user2118738 2013-03-01 10:37:54

+0

高兴地帮助:) – Dino 2013-03-01 10:41:09

+0

我也有两个数据库的示例问题,如果每个数据库相同的服务器主机它工作正常,但每一个差异它不工作 – Meas 2015-03-05 05:03:15

0

尝试在你的配置database.php中改变这个

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

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

我已经试过了,但仍然无法正常工作..我不知道什么是错的 – user2118738 2013-03-01 03:29:38

相关问题