Ben
Ben

Reputation: 6086

Codeigniter - Multiple database connections - local and remote

I am currently building an app whereby a user will be able to enter their own personal DB connections to utilise data from their MySQL database.

This means I will have two DB connections - my local (managing sessions etc) and the users remote one.

Can anyone advise on the best way to manage these two connections? I have looked at db groups - http://codeigniter.com/user_guide/database/connecting.html however the users db connection will be set by session vars, so I cannot put the details of the remote db in the config file.

I have tried manually setting a new db group within my class like:

        $db['foreign']['hostname'] = $this->session->userdata('hostname');
        $db['foreign']['username'] = $this->session->userdata('dbuser');
        $db['foreign']['password'] = $this->session->userdata('dbpassword');
        $db['foreign']['database'] = $this->session->userdata('dbname');
        $db['foreign']['dbdriver'] = "mysql";
        $db['foreign']['dbprefix'] = "";
        $db['foreign']['pconnect'] = FALSE;
        $db['foreign']['db_debug'] = TRUE;
        $db['foreign']['cache_on'] = FALSE;
        $db['foreign']['cachedir'] = "";
        $db['foreign']['char_set'] = "utf8";
        $db['foreign']['dbcollat'] = "utf8_general_ci";

        $foreign_db = $this->load->database('foreign', TRUE);

But i get an exception on the load line:

You have specified an invalid database connection group.

Can anyone advise how I can achieve this?

Many thanks, Ben.

Upvotes: 0

Views: 6332

Answers (3)

MrPHP
MrPHP

Reputation: 972

you could use a hook to get the session var's and populate the config vars pre-connection.

else you could manually call the second DB class from the controllers and use the session class to define the details ?

Upvotes: 0

Nenoco
Nenoco

Reputation: 83

In order to use the group calling syntax, the group has to be defined in the config file. CI includes the ability to pass the config directly in to the database loader. (Note: this might only be a CI 2+ feature)

You want something like this:

$db['hostname'] = $this->session->userdata('hostname');
$db['username'] = $this->session->userdata('dbuser');
$db['password'] = $this->session->userdata('dbpassword');
$db['database'] = $this->session->userdata('dbname');
$db['dbdriver'] = "mysql";
$db['dbprefix'] = "";
$db['pconnect'] = FALSE;
$db['db_debug'] = TRUE;
$db['cache_on'] = FALSE;
$db['cachedir'] = "";
$db['char_set'] = "utf8";
$db['dbcollat'] = "utf8_general_ci";

$foreign_db = $this->load->database($db, TRUE);

Upvotes: 3

Repox
Repox

Reputation: 15476

The 'foreign' group is probably not known to the system at the point you're trying to load it.

You shouldn't load anything in your config files; that's not what config files are for, which basically is the problem.

If you really need to autoload the foreign database, do it by extending the CI controller and load it in it's construct.

Upvotes: 0

Related Questions