1

I am trying to connect to a different database than what is in my database config file.

I've been able to do this with the following in my model:

        $wp['hostname'] = "localhost";
        $wp['username'] = "root";
        $wp['password'] = "";
        $wp['database'] = "transfer";
        $wp['dbdriver'] = "mysql";
        $wp['dbprefix'] = "";
        $wp['pconnect'] = FALSE;
        $wp['db_debug'] = TRUE;
        $wp['cache_on'] = FALSE;
        $wp['cachedir'] = "";
        $wp['char_set'] = "utf8";
        $wp['dbcollat'] = "utf8_general_ci";

        $wpDB = $this->load->database($wp, TRUE);

and then running queries like so: $query = $wpDB->get();

I can only get it to work when the config values are in the model itself (so there would be a lot of duplication). I've tried putting the config array in the constructor, but I get an error that it can't find it.

Where can I put the config array so I don't have to duplicate it and that's available throughout the model?

5 Answers 5

3

Database configuration usually goes in config/database.php. You can configure multiple database connections and store them with different group names:

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

$db['default']['hostname'] = 'host1';
$db['default']['username'] = 'user1';
$db['default']['password'] = '******';
$db['default']['database'] = 'my_db';

$db['other']['hostname'] = 'host2';
$db['other']['username'] = 'user2';
$db['other']['password'] = '******';
$db['other']['database'] = 'my_other_db;

The $active_group refers to the default group when the database class is loaded. To connect to another group in your model, you can use this in the model's __construct method:

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

While this doesn't play nicely with the more flexible $this->load->model('model_name', 'alias', $config) approach, it might be easier.

More info: http://codeigniter.com/user_guide/database/connecting.html

Sign up to request clarification or add additional context in comments.

Comments

2

i think that this can help you, directly from codeigniter forum :)

2 Comments

Links are OK in answers, but not by themselves. Please post the relevant information and use the link as reference.
@Madmartigan is right; your answer may be deleted if you don't edit and add some details. For more info, please search on Meta Stack Overflow for what is and is not an answer. Thanks.
2

There is a config directory in Code Igniter where you can add your own config file:

system/application/config

In that same directory there is an autoload.php file where you can specify which additional config files you want to load.

Comments

0

You can use the config file from the CodeIgniter.

To retrieve a config var, use:

$this->config->item('item name');

More info in the docs

Comments

0

Since your config data appears to be database related, store them in

system/application/config/database.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.