5

I have a CodeIgniter and some Mysql database. But know I want to set the another database for working, and after some actions do disable it. For example, I have function make_some_actions_with_db(), and now I need to have something like that:

public function action()
{
//load db
make_some_actions_with_db();
//disable db
}

So, now I need to know how I can set the new default db and how I can set another (first) db. Thank you.

UPDATED: It doesn't work:

   public function update_record_insert_test()
    {
        if ($this->facebook->getUser())
        {
            $another_db_settings['hostname'] = 'localhost';
            $another_db_settings['username'] = 'root';
            $another_db_settings['password'] = '';
            $another_db_settings['database'] = 'name';
            $another_db_settings['dbdriver'] = "mysql";
            $another_db_settings['dbprefix'] = "";
            $another_db_settings['pconnect'] = TRUE;
            $another_db_settings['db_debug'] = TRUE;
            $another_db_settings['cache_on'] = FALSE;
            $another_db_settings['cachedir'] = "";
            $another_db_settings['char_set'] = "utf8";
            $another_db_settings['dbcollat'] = "utf8_general_ci";
            $this->load->database($another_db_settings);

            $_POST['id']='AccountPagesView.a_book/1000';
            $_POST['value']='test';
            $_POST['old_value']='not';
            $welcome=new Welcome();
            $welcome->update_record();
        }
        else
        {
            $url=$this->facebook->getLoginUrl(array('next' => base_url().'update_record_insert_test'));
            redirect($url);
        }
    }

2 Answers 2

10

In your config/database.php your database is configured to the 'default' group, like so:

$db['default']['hostname'] = 'host';
$db['default']['username'] = 'username';
$db['default']['password'] = 'password';
$db['default']['database'] = 'database';

This allows you to specify other groups like this:

$db['second']['hostname'] = 'host';
$db['second']['username'] = 'username';
$db['second']['password'] = 'password';
$db['second']['database'] = 'database';

$db['third']['hostname'] = 'host';
$db['third']['username'] = 'username';
$db['third']['password'] = 'password';
$db['third']['database'] = 'database';

You can then connect to another database by using:

$defaultDB = $this->load->database('default', TRUE);
$secondDB = $this->load->database('second', TRUE);

By passing true as the second value, it will return the database object, allowing you to safely use the same methods for each database, like so:

$default->get('table');
$second->get('different_table');
3
  • Now I make all request using $this->db->get(...), and I can't change this code, but there is the same db with the same tables, I need to change the default db this this one. How I can do it? For example, now I have "db1" database, and the second is "db1_copy" - I need that all request similar $this->db->... will be executed with "db1_copy". Commented Jun 27, 2012 at 13:54
  • If you can change the config, change the details for the default group to the details of 'db1_copy'. That should work?
    – cchana
    Commented Jun 27, 2012 at 13:55
  • 1
    From what I'm understanding from this, are you setting up environments? Like development and production environments? In this case, why not place an IF statement in your database config file to detect which database config to use. Something like $_SERVER['SERVER_NAME'] == 'something' => $active_group = 'db1'
    – Steven Lu
    Commented Jun 27, 2012 at 15:29
0

If you want to change the default database connection (which I don't recommend) you can do :

$another_db_settings['hostname'] = '';
$another_db_settings['username'] = '';
$another_db_settings['password'] = '';
$another_db_settings['database'] = '';
$another_db_settings['dbdriver'] = "mysql";
$another_db_settings['dbprefix'] = "";
$another_db_settings['pconnect'] = TRUE;
$another_db_settings['db_debug'] = TRUE;
$another_db_settings['cache_on'] = FALSE;
$another_db_settings['cachedir'] = "";
$another_db_settings['char_set'] = "utf8";
$another_db_settings['dbcollat'] = "utf8_general_ci";
$this->load->database($another_db_settings);

Then the new database can be accessed via $this->db, but I would recommend doing what cchana suggested and assign the new connection to another variable via

$new_db = $this->load->database($another_db_settings, TRUE);
3
  • Your recommendation doesn't work. Please check updated question. Commented Jun 27, 2012 at 14:12
  • Could you show the Welcome class? I don't see why you would need another database connection in this case.
    – marshall
    Commented Jun 27, 2012 at 14:19
  • update_record() function in Welcome class does some actions with main database. I need to test it, and I want to use for it copy of main db. Commented Jun 27, 2012 at 14:24

Your Answer

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

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