Connection with multiple database with CI(Code Igniter) need a small trick. First you have to add lines of code in your configuration file in application/config/database.php. Add as much database configuration as you need
$active_group = "default";
$active_record = TRUE;
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "ci_practice";
$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['second_db']['hostname'] = "localhost";
$db['second_db']['username'] = "root";
$db['second_db']['password'] = "";
$db['second_db']['database'] = "dev_job";
$db['second_db']['dbdriver'] = "mysql";
$db['second_db']['dbprefix'] = "";
$db['second_db']['pconnect'] = FALSE;
$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";
So you have configured two different databases with their hostname, username and password. Moreover, you named them as default and second_db. In addition, you can choose any you like and as much database as you wish.
Now your class code application/controllers.test.php.
class Test extends Controller {
function __constructor()
{
parent::Controller();
$this->load->helper('url');
}
function duel_db2()
{
$this->load->model('test_model');
$user_data = $this->test_model->get_country(1);
print_r($user_data);
$user_data = $this->test_model->get_feedback(1);
print_r($user_data);
$this->load->database('default'); //Get Back into default.
}
}
finally your model (application/test_model.php) where from actually you connect multiple databases.
function get_country()
{
$DBOne = $this->load->database('second_db', TRUE);
$query = $DBOne
->from('countries')
->limit(10)
->where('id' , 10)
->get();
if ($query->num_rows() > 0)
{
$rows = $query->row_array();
return $rows;
}
return false;
}
function get_feedback($id)
{
$DBTwo = $this->load->database('default', TRUE);
$query = $DBTwo
->from('feedback')
->limit(10)
->where('id' , $id)
->get();
if ($query->num_rows() > 0)
{
$rows = $query->row_array();
return $rows;
}
return false;
}
Finally how it works? Lets say you call test controller as www.your-site.com/test/duel_db2
the duel_db2() function call test_model twice. first get_country(1). Lets have a closer look in this function. You connect default database by
$this->load->database('default');
and gets data from first database(ci_practice here). Second function call get_feedback() let the system able to connect with second database(dev_job here) by $DBTwo = $this->load->database('default', TRUE);
Keep in mind both of your connection is still alive
Enjoy with mutiple database connection simultaniouly with excellent framework (CI).
ไม่มีความคิดเห็น:
แสดงความคิดเห็น