How to load another database using CodeIgniter?

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

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

So now I need to know how I can install a new db by default and how I can install another (first) db. Thank.

UPDATED: This does not 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);
        }
    }
+5
source share
2 answers

In yours, config/database.phpyour database is set to the default group, for example:

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

This allows you to specify other groups, such as:

$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';

Then you can connect to another database using:

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

true , , , :

$default->get('table');
$second->get('different_table');
+9

( ), :

$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);

$this- > db, , cchana,

$new_db = $this->load->database($another_db_settings, TRUE);
0

All Articles