Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在codeigniter 3中切换数据库_Php_Mysql_Codeigniter_Codeigniter 3 - Fatal编程技术网

Php 在codeigniter 3中切换数据库

Php 在codeigniter 3中切换数据库,php,mysql,codeigniter,codeigniter-3,Php,Mysql,Codeigniter,Codeigniter 3,我正在尝试为不同的组织/用户使用/更改数据库。在master数据库中,我有不同组织的db_名称,我使用以下代码在会话中存储db_名称 $condition = "subdomain =" . "'" . $data['subdomain'] . "' AND " . "status =1"; $this->db->select('id, db_name'); $this->db->from('sublogin'); $this->db->where($con

我正在尝试为不同的组织/用户使用/更改数据库。在master数据库中,我有不同组织的db_名称,我使用以下代码在会话中存储db_名称

$condition = "subdomain =" . "'" . $data['subdomain'] . "' AND " . "status =1";

$this->db->select('id, db_name');
$this->db->from('sublogin');
$this->db->where($condition);
$this->db->limit(1);

$db_name = $this->db->get()->row()->db_name;

// SET db_name for session
$this->session->set_userdata('db_name', $db_name);
在另一个模型中,切换数据库

$db_name = $this->session->userdata('db_name');
$this->db->db_select($db_name); 
它在本地主机上工作,但在我的live server上不工作,它给出错误:

发生数据库错误 错误号码:1146 表“db694230824.users”不存在

我试图回显当前的数据库,它说数据库没有更改

echo $this->db->database;
die(); 

1个解决方案。

您可以使用多个数据库,如下所示:

$this->db = $this->load->database('default', TRUE);
$this->anotherDb = $this->load->database('anotherDb ', TRUE);
$this->anotherDb = $this->load->database('anotherDb', true);
或者,如果指定codeigniter超级对象:

public function __construct($params) {

   $this->CI = & get_instance();
   $this->CI->db = $this->CI->load->database('default', TRUE);
   $this->CI->anotherDb = $this->CI->load->database('anotherDb', TRUE);
}
2解决方案

它在早期工作过,不能100%确定它是否仍然有效,但看起来这个bug仍然存在于CI中。 只需在/system/database/DB_driver.php文件的simple_query函数中添加1行代码:

public function simple_query($sql)
{
    if ( ! $this->conn_id)
    {
        $this->initialize();
    }

    $this->db_select(); // add this
    return $this->_execute($sql);
}
它应该允许您在以下模型中使用其他数据库:

$this->db = $this->load->database('default', TRUE);
$this->anotherDb = $this->load->database('anotherDb ', TRUE);
$this->anotherDb = $this->load->database('anotherDb', true);
不要忘记在/application/config/database.php文件中添加其他数据库的连接设置,方法与默认数据库类似:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    ...
);

$db['anotherDb'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    ...
);

1解决方案。

您可以使用多个数据库,如下所示:

$this->db = $this->load->database('default', TRUE);
$this->anotherDb = $this->load->database('anotherDb ', TRUE);
$this->anotherDb = $this->load->database('anotherDb', true);
或者,如果指定codeigniter超级对象:

public function __construct($params) {

   $this->CI = & get_instance();
   $this->CI->db = $this->CI->load->database('default', TRUE);
   $this->CI->anotherDb = $this->CI->load->database('anotherDb', TRUE);
}
2解决方案

它在早期工作过,不能100%确定它是否仍然有效,但看起来这个bug仍然存在于CI中。 只需在/system/database/DB_driver.php文件的simple_query函数中添加1行代码:

public function simple_query($sql)
{
    if ( ! $this->conn_id)
    {
        $this->initialize();
    }

    $this->db_select(); // add this
    return $this->_execute($sql);
}
它应该允许您在以下模型中使用其他数据库:

$this->db = $this->load->database('default', TRUE);
$this->anotherDb = $this->load->database('anotherDb ', TRUE);
$this->anotherDb = $this->load->database('anotherDb', true);
不要忘记在/application/config/database.php文件中添加其他数据库的连接设置,方法与默认数据库类似:

$db['default'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    ...
);

$db['anotherDb'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    ...
);


您的数据库用户是否具有访问这两个数据库的正确权限?让我检查一下权限。您运气好吗?否,支持人员将在48小时后回复。它在我的本地主机上运行良好,因此我认为它的权限问题正如您所提到的。因此,我需要等待他们。我在这里很不走运,我所有的问题都没有答案,并且有(-)个否定点。您的数据库用户是否拥有访问这两个数据库的正确权限?让我检查权限。您运气好吗?不,支持人员将在48小时后答复。它在我的本地主机上运行良好,因此我认为它的权限问题正如您所提到的。所以,我需要等待他们。我在这里很不走运,我所有的问题都没有答案,并且有(-)个否定点。谢谢你的回答,这将是我的第二个选择,因为$this->db_选择(mydb);在localhost中对我来说很好。我正在等待主机公司回复,并给予其他数据库上的dbuser权限。仍在等待主机公司。使用这个解决方案,我需要定义每个数据库,我的应用程序将有多个数据库,可能是100个。所以,我想我必须使用db_select()。是的,第二种解决方案可能会更舒适,并提供灵活的选项。不过,这一切都取决于应用程序的设计。在某些情况下,1解决方案可能更好。我不想硬编码我的数据库名称,因为我的应用程序中将有更多的数据库。1所学校的1个数据库。实现了第一个解决方案,还使用了动态数据库名称。感谢您的回答,这将是我的第二个选项,作为$this->db\u选择(mydb);在localhost中对我来说很好。我正在等待主机公司回复,并给予其他数据库上的dbuser权限。仍在等待主机公司。使用这个解决方案,我需要定义每个数据库,我的应用程序将有多个数据库,可能是100个。所以,我想我必须使用db_select()。是的,第二种解决方案可能会更舒适,并提供灵活的选项。不过,这一切都取决于应用程序的设计。在某些情况下,1解决方案可能更好。我不想硬编码我的数据库名称,因为我的应用程序中将有更多的数据库。1所学校的1个数据库。实现了第一个解决方案,还使用了动态数据库名称。