php - how to set multiple databases with same localhost, root and password -
application/config/ database.php, how can set database same localhost, please help! in advance
$active_group = 'default'; $active_record = true; $db['default'] = array( 'dsn' => '', 'hostname' => 'localhost', 'username' => 'root', 'password' => '', 'database' => 'business_permit', '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 );
the best way use different database groups. if want keep using master database usual ($this->db
) turn off persistent connection configuration option secondary database(s). master database should work persistent connection :
master database
$db['default']['hostname'] = "localhost"; $db['default']['username'] = "root"; $db['default']['password'] = ""; $db['default']['database'] = "database_name"; $db['default']['dbdriver'] = "mysql"; $db['default']['dbprefix'] = ""; $db['default']['pconnect'] = true; $db['default']['db_debug'] = false; $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;
secondary database (notice pconnect set false)
$db['otherdb']['hostname'] = "localhost"; $db['otherdb']['username'] = "root"; $db['otherdb']['password'] = ""; $db['otherdb']['database'] = "other_database_name"; $db['otherdb']['dbdriver'] = "mysql"; $db['otherdb']['dbprefix'] = ""; $db['otherdb']['pconnect'] = false; $db['otherdb']['db_debug'] = false; $db['otherdb']['cache_on'] = false; $db['otherdb']['cachedir'] = ""; $db['otherdb']['char_set'] = "utf8"; $db['otherdb']['dbcollat'] = "utf8_general_ci"; $db['otherdb']['swap_pre'] = ""; $db['otherdb']['autoinit'] = true; $db['otherdb']['stricton'] = false;
then can use secondary databases database objects while using master database usual :
// use master dataabse $users = $this->db->get('users'); // connect secondary database $otherdb = $this->load->database('otherdb', true); $stuff = $otherdb->get('struff'); $otherdb->insert_batch('users', $users->result_array()); // keep using master database usual, example insert stuff other database $this->db->insert_batch('stuff', $stuff->result_array());
hopes answered question. google first before ask question... ;)
Comments
Post a Comment