Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm using 2 different databases on my CI app.

I created 2 users on mysql, and granted user1 to db1 all privileges, then user2 to db2 all privileges with SQLpro client.

The database.php:

$active_group = 'default';
$active_record = TRUE;


    $db['default']['hostname'] = 'localhost';
    $db['default']['username'] = '***1';
    $db['default']['password'] = '***1';
    $db['default']['database'] = 'ci_users';
    $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_unicode_ci';
    $db['default']['swap_pre'] = '';
    $db['default']['autoinit'] = TRUE;
    $db['default']['stricton'] = FALSE;

    $db['keys']['hostname'] = 'localhost';
    $db['keys']['username'] = '***2';
    $db['keys']['password'] = '***2';
    $db['keys']['database'] = 'ci_keys';
    $db['keys']['dbdriver'] = 'mysql';
    $db['keys']['dbprefix'] = '';
    $db['keys']['pconnect'] = TRUE;
    $db['keys']['db_debug'] = TRUE;
    $db['keys']['cache_on'] = FALSE;
    $db['keys']['cachedir'] = '';
    $db['keys']['char_set'] = 'utf8';
    $db['keys']['dbcollat'] = 'utf8_unicode_ci';
    $db['keys']['swap_pre'] = '';
    $db['keys']['autoinit'] = TRUE;
    $db['keys']['stricton'] = FALSE;

The models:

<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_Users extends CI_Model {
    function Model_Users()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database('default',true);
    }
etc...


<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_Keys extends CI_Model {
    function Model_Keys()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database('keys',true);
    }
etc...

So when using both models in same controller/method the model_users works great but model_keys returns error, cause seems it loads anyway the model_users db1 instead ofl oading db2:

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/app/application/models/model_keys.php:2)

Filename: core/Common.php

Line Number: 442
A Database Error Occurred

Error Number: 1146

Table 'ci_users.ci_keys' doesn't exist //this should be ci_keys.ci_keys

does anyone has idea?

I edited code in model_keys:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Model_Keys extends CI_Model {

    function Model_Keys()
    {
        // Call the Model constructor
        parent::__construct();
        $this->db_keys = $this->load->database('keys',true);
    } 

now db works but i receive session error on model :O

Message: Cannot modify header information - headers already sent by (output started at /Applications/XAMPP/xamppfiles/htdocs/site/application/models/model_keys.php:2)

in controller i'm doing this:

$this->load->model('model_users');
$this->model_users->insert(bla bla);

$this->load->model('model_keys');
$this->model_keys->insert(bla bla);

$this->session->set_userdata(array(bl bla);

redirect(some url);
share|improve this question

1 Answer

up vote 3 down vote accepted

Below line is problem

$this->load->database('keys',true);

If you pass second argument TRUE, then it will return database object and it will not work with ActiveRecord.

Here is example

$DB1 = $this->load->database('group_one', TRUE);

//Then you can't below function
$this->db->query();

You will instead use:

$DB1->query();
share|improve this answer
thx, i edited question cause i'm receiving error with session it's strange – sbaaaang Oct 19 '12 at 9:00
error says header output start in model_keys.php before redirection. try to put ob_start() before redirect(some url); – GBD Oct 19 '12 at 9:14
tryed but it doesn't produces nothing and doesn't fix nothing :P – sbaaaang Oct 19 '12 at 9:18
what is on line 2 in model_keys.php ? – GBD Oct 19 '12 at 9:21
1  
Yes.. because somewhere output already started to sent to browser. – GBD Oct 19 '12 at 9:32
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.