Multiple Koneksi Database diCode Igniter

Sekedar berbagi, berikut ini cara mengkoneksikan aplikasi web ke dua database pada framework code igniter. Bawaan default dari CI terdapat pada file system/app/config/database.php berupa :


$db['default']['hostname'] = "localhost";
 $db['default']['username'] = "root";
 $db['default']['password'] = "";
 $db['default']['database'] = "ujicoba_db";
 $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";

selanjutnya, bila ingin menambah koneksi 1 lagi ke server database lain, perlu menambah konfigurasi koneksi dibawah baris tadi dengan array bukan default.


$db['second_db']['hostname'] = "localhost";
 $db['second_db']['username'] = "root";
 $db['second_db']['password'] = "";
 $db['second_db']['database'] = "kampus";
 $db['second_db']['dbdriver'] = "mysql";
 $db['second_db']['dbprefix'] = "";
 $db['second_db']['pconnect'] = TRUE;
 $db['second_db']['db_debug'] = TRUE;
 $db['second_db']['cache_on'] = FALSE;
 $db['second_db']['cachedir'] = "";
 $db['second_db']['char_set'] = "utf8";
 $db['second_db']['dbcollat'] = "utf8_general_ci";

 

Kemudian cara pemanggilan koneksi tambahan tadi sebagai berikut :


$this->second_db=$this->load->database('second_db',TRUE);
 $this->second_db->where('nim', $nim);
 $query = $this->second_db->getwhere('krs');
 return $query;

 

Semoga bermanfaat😉

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: