Welcome Guest, Not a member yet? Register   Sign In
multi database connection problem
#1

[eluser]mic[/eluser]
Hi,

I am having a problem while connecting to multi databases with CI 2.0.0.

currently i have two databases one for the normal site data and another for a demo account. I want to be able to swap between the databases based on a session variable being set or not.

so far this is the code i have tried:
Code:
class Admin extends CI_Controller
{    
    
    private $db_group_name = "default";
    
    function __construct()
    {
        
        parent::__construct();

        $this->db_group_name = ($this->session->userdata("admin_demo_user") !== false? "demo": "default");
        $this->db->close();
        $this->load->database($this->db_group_name);
        
    }
}
and my database.php config file:
Code:
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'cmc';
$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['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = FALSE;

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

can anyone point me in the right direction with this? I am now at a loss as to what i should now be changing to get this to work.

Thanks Mic

EDIT ---------------------------------------------------------------------

PS: I have this included in the constructors in all the controller, models and libraries that require the connection but i can oonly get it to connect to the default database.




Theme © iAndrew 2016 - Forum software by © MyBB