如何使用CodeIgniter加载另一个数据库?

时间:2012-06-27 13:29:50

标签: php codeigniter

我有一个CodeIgniter和一些Mysql数据库。但是知道我想设置另一个数据库工作,并在一些操作后禁用它。例如,我有函数make_some_actions_with_db(),现在我需要有类似的东西:

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

所以,现在我需要知道如何设置新的默认数据库以及如何设置另一个(第一个)数据库。谢谢。

更新: 它不起作用:

   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 个答案:

答案 0 :(得分:9)

config/database.php您的数据库配置为'默认'小组,像这样:

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

这允许您指定其他组,如下所示:

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

然后,您可以使用以下方法连接到另一个数据库:

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

通过传递true作为第二个值,它将返回数据库对象,允许您为每个数据库安全地使用相同的方法,如下所示:

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

答案 1 :(得分:0)

如果要更改默认数据库连接(我不建议),您可以执行以下操作:

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