5

我有一个 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);
        }
    }
4

2 回答 2

10

在您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');
于 2012-06-27T13:49:27.413 回答
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);
于 2012-06-27T13:52:34.013 回答