首页 > 解决方案 > Is there any way in CodeIgniter to remove dbprefix from the query?

问题描述

I'm building a CI application, and I need to use 2 different databases (mysql & mssql). When i try to run a simple query to the mssql DB then CI adds the dbprefix to the table names, so it ends up to an error. How is it possible to prevent CI adding the prefix? There is no dbprefix set in database.php file

Query in Model

    public function getAccName($uid) 

{
            $this->load->database('mssqlsrv',TRUE);

           return $this->db->where("uid", $uid)
    ->select("account")->get("user_account");                  
}

标签: phpcodeigniter

解决方案


要正确回答这个问题:

在 Codeigniter 中处理多个数据库连接时,每个连接都需要分配给一个变量,例如:

$DB1 = $this->load->database('mssqlsrv', TRUE);
$DB2 = $this->load->database('group_two', TRUE);

然后你可以用

$DB1->where("uid", $uid)->select("account")->get("user_account");

代替

$this->db->where("uid", $uid)->select("account")->get("user_account");

有关这方面的更多信息,请参阅https://www.codeigniter.com/userguide3/database/connecting.html


推荐阅读