简体   繁体   中英

how to connect database multiple server in codeigniter?

i build project in my local server but file database save in another server. i try edit in config database.php like this

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

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '';
$db['default']['password'] = '';
$db['default']['database'] = '';
$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['database_two']['hostname'] = 'xxx.xxx.xx.xx';
$db['database_two']['username'] = 'vtis';
$db['database_two']['password'] = '';
$db['database_two']['database'] = 'vtis';
$db['database_two']['dbdriver'] = 'postgre';
$db['database_two']['dbprefix'] = '';
$db['database_two']['pconnect'] = TRUE;
$db['database_two']['db_debug'] = TRUE;
$db['database_two']['cache_on'] = FALSE;
$db['database_two']['cachedir'] = '';
$db['database_two']['char_set'] = 'utf8';
$db['database_two']['dbcollat'] = 'utf8_general_ci';
$db['database_two']['swap_pre'] = '';
$db['database_two']['autoinit'] = TRUE;
$db['database_two']['stricton'] = FALSE;
$db['database_two']['port'] = 5432;

but i don't know next step after that. can you tell me step by step how to connect database in another server with codeigniter?

munit_list.php

*/
class Munit_list extends CI_Model
{

    private $tbl_unit_list = 'unit_list';
    function __construct()
    {
        parent::__construct();
        $CI=&get_instance();
        $CI->database_two = $this->load->database('database_two', TRUE);
        $this->database_two =& $CI->database_two; 
    }

    function get_all_unit_list()
    {
        $this->database_two->order_by('unit_id','desc');
        $data = $this->database_two->get($this->tbl_unit_list);
        return $data->result();
    }

}

i try model like this but its not working

its easy. Just this 3 lines:

$CI=&get_instance;
$CI->database_two = $this->load->database('database_two', TRUE);
$this->database_two =& $CI->database_two; 

in your model in the construct function just add the database(s) you want to use. You can add as many as you like, as long as they are defined in your database file.

class Munit_list extends CI_Model {

function __construct() {
    parent::__construct();
    $this->database_two= $this->load->database('database_two', TRUE); 
}

$this->database_two (can be any name you want to put it);
$this->load->database('database_two', TRUE); 'database_two' is the name you defined on your database file and the TRUE param is for the function to return the database object.

Then you can use it like:

function get_all_units_list(){
    $this->database_two->select("*");
    ....
    ....
}

More information in CodeIgniter Connecting to your databases

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM