1

I am using this code in CodeIgniter to add a database:

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '12345',
    'database' => 'saas',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

How can I add a second database? And How can i use them simultaneously? Any help would be greatly appreciated!

Muhammad Rohail
  • 265
  • 4
  • 14
  • 1
    possible duplicate of [Codeigniter - multiple database connections](http://stackoverflow.com/questions/8268853/codeigniter-multiple-database-connections) – Manwal Aug 12 '15 at 14:09

1 Answers1

-1

From The Documentation:

$db['test'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => 'root',
    'password' => '',
    'database' => 'database_name',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => TRUE,
    'db_debug' => TRUE,
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'compress' => FALSE,
    'encrypt' => FALSE,
    'stricton' => FALSE,
    'failover' => array()
);

In your model you can write:

function method()
{
  $test = $this->load->database('test', TRUE); // the TRUE paramater tells CI that you'd like to return the database object.

  $query = $test->select('first_name, last_name')->get('person');
  var_dump($query);
}

Read More (http://www.codeigniter.com/user_guide/database/configuration.html)

Njuguna Mureithi
  • 3,506
  • 1
  • 21
  • 41
  • you are not explaining how to load a second database – CodeGodie Aug 12 '15 at 14:26
  • @CodeGodie What is not understandable. Unless someone does not understand CI. `$test = $this->load->database('test', TRUE);` and `$default = $this->load->database();` – Njuguna Mureithi Aug 12 '15 at 19:09
  • Yes i downvoted because initially you did not have any source or explanations, it seems now you fixed it(however you can do a better job at explaining) Unfortunately when i tried to upvote, my vote is now locked. – CodeGodie Aug 13 '15 at 11:32