CodeIgniter Forums

Full Version: creating multiple database connection in codeigniter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

El Forum

[eluser]Bigil Michael[/eluser]
modify database.php like this
Code:
$active_group = 'default';
$active_record = TRUE;

$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'dbname';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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;

$db['test']['hostname'] = 'localhost';
$db['test']['username'] = 'username';
$db['test']['password'] = 'password';
$db['test']['database'] = 'dbname';
$db['test']['dbdriver'] = 'mysql';
$db['test']['dbprefix'] = '';
$db['test']['pconnect'] = FALSE;
$db['test']['db_debug'] = FALSE;
$db['test']['cache_on'] = FALSE;
$db['test']['cachedir'] = '';
$db['test']['char_set'] = 'utf8';
$db['test']['dbcollat'] = 'utf8_general_ci';
$db['test']['swap_pre'] = '';
$db['test']['autoinit'] = TRUE;
$db['test']['stricton'] = FALSE;
modify your model like this
Code:
class Testmodel extends Model {
public $DB2;
function Testmodel(){
  parent::Model();
  $this->DB2 = $this->load->database('test', TRUE);
}
function get_cities()
    {
  $this->DB2->order_by('id');
  $this->DB2->select('id, name');
  $result_areas = $this->DB2->get('cities');
  return $result_areas->result();
    }

El Forum

[eluser]InsiteFX[/eluser]
Here's one I did for changing the database config for local and live server!
Code:
if (defined('ENVIRONMENT'))
{
switch (ENVIRONMENT)
{
  case 'development':
   $active_group  = 'default';
  break;

  case 'testing':
  case 'production':
   $active_group = 'live';
  break;

  default:
   $active_group  = 'default';
  break;
}
}
else
{
$active_group  = 'default';
}

$active_record = TRUE;

// Local Server.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = '';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$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['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

// Live Server.
$db['live']['hostname'] = 'localhost';
$db['live']['username'] = 'username';
$db['live']['password'] = 'password';
$db['live']['database'] = '';
$db['live']['dbdriver'] = 'mysql';
$db['live']['dbprefix'] = '';
$db['live']['pconnect'] = FALSE;
$db['live']['db_debug'] = TRUE;
$db['live']['cache_on'] = FALSE;
$db['live']['cachedir'] = '';
$db['live']['char_set'] = 'utf8';
$db['live']['dbcollat'] = 'utf8_general_ci';
$db['live']['swap_pre'] = '';
$db['live']['autoinit'] = TRUE;
$db['live']['stricton'] = FALSE;

El Forum

[eluser]Matalina[/eluser]
If you are creating production and development databases then just make a folder for each environment and drop in a database.php file into each one and make the changes to that. CI already has that functionality.