1

I have a problem with multiple db connections at Codeigniter. At my database.php i configured two databases.

$active_group = 'cms';
$active_record = FALSE;
     $db['cms']['hostname'] = 'localhost';
    $db['cms']['username'] = 'yoloo_cms';
    $db['cms']['password'] = 'password'; 
    $db['cms']['database'] = 'yoloo_cms'; 
    $db['cms']['dbdriver'] = 'mysql';
    $db['cms']['dbprefix'] = '';
    $db['cms']['pconnect'] = TRUE;
    $db['cms']['db_debug'] = TRUE;
    $db['cms']['cache_on'] = FALSE;
    $db['cms']['cachedir'] = '';
    $db['cms']['char_set'] = 'utf8';
    $db['cms']['dbcollat'] = 'utf8_general_ci';
    $db['cms']['swap_pre'] = '';
    $db['cms']['autoinit'] = TRUE;
    $db['cms']['stricton'] = FALSE;

    $db['hazeleger']['hostname'] = 'localhost';
    $db['hazeleger']['username'] = 'yoloo_websites';
    $db['hazeleger']['password'] = 'password2'; 
    $db['hazeleger']['database'] = 'yoloo_hazeleger'; 
    $db['hazeleger']['dbdriver'] = 'mysql';
    $db['hazeleger']['dbprefix'] = '';
    $db['hazeleger']['pconnect'] = TRUE;
    $db['hazeleger']['db_debug'] = TRUE;
    $db['hazeleger']['cache_on'] = FALSE;
    $db['hazeleger']['cachedir'] = '';
    $db['hazeleger']['char_set'] = 'utf8';
    $db['hazeleger']['dbcollat'] = 'utf8_general_ci';
    $db['hazeleger']['swap_pre'] = '';
    $db['hazeleger']['autoinit'] = TRUE;
    $db['hazeleger']['stricton'] = FALSE;

At my model i use this when i want to connect to a other db than the usual one:

function __construct()
{
  parent::__construct();
  $this->load->database('hazeleger',TRUE);
}

But at all time codeigniter connects to cms. When i remove

$active_group = 'cms';
$active_record = FALSE;

Codeingiter gives an error. When i tried this

function __construct()
{
  parent::__construct();
  $db2 = $this->load->database('hazeleger',TRUE);
}

function test()
{
      $query  = "SELECT * FROM cms_modules";
      $result = $db2->db->query($query);
      return $db2->result();
}

It gives an error. Variabele db2 does not exist. I just want to choose, at every model, wich db i want to connect to. But is doesn,t work. Does somebody know, how i can work with different databases at models.

Thank you very much!!

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
JelleP
  • 976
  • 7
  • 20
  • 39

4 Answers4

5

You have to save the variable $db2 as a class field. The you can access $this->db2 ...

Tobias
  • 9,170
  • 3
  • 24
  • 30
1

for future visitors' reference, the load would be along these lines:

$this->db2 = $this->load->database('hazeleger',true);
steve
  • 196
  • 1
  • 5
1

You have to change db2 class

    $query  = "SELECT * FROM cms_modules";
    $result = $this->db2->query($query);
    return result();
Arun
  • 750
  • 5
  • 12
1

This happens because you have most probably set in /application/config/autoload.php that the database library is automatically loaded/created.

Open autoload.php and look for this line:

 $autoload['libraries'] = array('database');

Remove 'database' from the array and save. Now it should be working in your controllers as intended.

Deckard
  • 2,384
  • 1
  • 19
  • 35