Welcome Guest, Not a member yet? Register   Sign In
Multiple Database Connections
#10

[eluser]Sumon[/eluser]
Finally i sort it out how to access multiple database simultaneously.
Here is my config\database.php
Code:
$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";

here is my controller
Code:
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.
}

and finally this is my model(test_model.php)
Code:
function get_country()
{
  $DBTwo = $this->load->database('second_db', TRUE);
  $query = $DBTwo
                ->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)
{
  $DBOne = $this->load->database('default', TRUE);
  $query = $DBOne
                ->from('feedback')
                ->limit(10)
                ->where('id' , $id)
                ->get();
  if ($query->num_rows() > 0)
  {
    $rows = $query->row_array();
    return $rows;
  }
  return false;
}


Messages In This Thread
Multiple Database Connections - by El Forum - 06-16-2008, 01:49 AM
Multiple Database Connections - by El Forum - 09-26-2008, 04:12 PM
Multiple Database Connections - by El Forum - 09-26-2008, 06:24 PM
Multiple Database Connections - by El Forum - 10-01-2008, 03:32 PM
Multiple Database Connections - by El Forum - 10-01-2008, 04:42 PM
Multiple Database Connections - by El Forum - 10-01-2008, 05:41 PM
Multiple Database Connections - by El Forum - 10-02-2008, 10:38 AM
Multiple Database Connections - by El Forum - 10-02-2008, 04:24 PM
Multiple Database Connections - by El Forum - 10-03-2008, 05:58 AM
Multiple Database Connections - by El Forum - 10-04-2008, 07:09 AM



Theme © iAndrew 2016 - Forum software by © MyBB