How to load 2 databases in one single coding using Codeigniter?
- Go to application/config/database.php .
- Create another database config like default database.
//Default database configuration$db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'db_username', 'password' => 'db_password', 'database' => 'db_name', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE); //Another database configuration$db['another_db'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'db_username', 'password' => 'db_password', 'database' => 'db_name2', 'dbdriver' => 'mysqli', 'dbprefix' => '', 'pconnect' => FALSE, 'db_debug' => (ENVIRONMENT !== 'production'), 'cache_on' => FALSE, 'cachedir' => '', 'char_set' => 'utf8', 'dbcollat' => 'utf8_general_ci', 'swap_pre' => '', 'encrypt' => FALSE, 'compress' => FALSE, 'stricton' => FALSE, 'failover' => array(), 'save_queries' => TRUE);
3. Now, u can load your second database.
//Load another database$DB2 = $this->load->database('another_db', TRUE);
4. After that, you can use your database like usual.
//Default database query$this->db->select('first_name, last_name');$this->db->from('users');$this->db->where('id', 99);$query = $this->db->get(); //Another database query$DB2->select('image');$DB2->from('cdn_images');$DB2->where('id', 25);$query = $DB2->get();
*Note : for more information can go to https://www.codexworld.com/connect-use-multiple-database-codeigniter/
Comments
Post a Comment