0

i have an application which need to be connected to 2 different db and in different server. is there any other settings i have to do beside doing it in in database.php? i wrote this in my code to connect to both 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

but when i tried to select data from the server db,, nothing happened.. there is no problem selecting data from local db though.. Can anyone help me?

Here is my 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;

do i have to include port in the server's 'hostname'?

2
  • did you set both configs for db in database.php? Can you show us your database.php also? Commented Feb 28, 2013 at 8:26
  • yes, i have, i've just include it now.. i don't think anything's wrong with it. Commented Mar 1, 2013 at 3:30

2 Answers 2

1

My whole database setup sample

// My 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;

In controller, loading database

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

In model, you can call

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

or

$this->sitedb->query('your query');
Sign up to request clarification or add additional context in comments.

2 Comments

it works!! thank you. i am actually new in php and CI, so thank you very much :)
I also got the sample problem with two databases if each database the same server host it work fine but each one difference it doesn't work
0

Try changing this in your config database.php

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

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

1 Comment

i have tried it,, but still doesn't work.. i don't know what's wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.