php Codeigniter - 使用多个数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/634291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:19:43  来源:igfitidea点击:

Codeigniter - Using Multiple Databases

phpdatabasecodeigniter

提问by Pedro

database.php:

数据库.php:

$db['default']['hostname'] = "192.168.2.104";
$db['default']['username'] = "webuser";
$db['default']['password'] = "----";
$db['default']['database'] = "vad";
$db['default']['dbdriver'] = "mysql";
$db['default']['dbprefix'] = "";
$db['default']['pconnect'] = TRUE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = "";
$db['default']['char_set'] = "utf8";
$db['default']['dbcollat'] = "utf8_general_ci";

$db['stats']['hostname'] = "192.168.2.104";
$db['stats']['username'] = "webuser";
$db['stats']['password'] = "---";
$db['stats']['database'] = "vad_stats";
$db['stats']['dbdriver'] = "mysql";
$db['stats']['dbprefix'] = "";
$db['stats']['pconnect'] = TRUE;
$db['stats']['db_debug'] = TRUE;
$db['stats']['cache_on'] = FALSE;
$db['stats']['cachedir'] = "";
$db['stats']['char_set'] = "utf8";
$db['stats']['dbcollat'] = "utf8_general_ci";

The issue is I can only define in the configuration one $active_group, default, or stats. I followed the CodeIgniter documentation and I added the following:

问题是我只能在配置中定义 one $active_group、default 或 stats。我遵循了 CodeIgniter 文档并添加了以下内容:

$DB2 = $this->load->database('stats', TRUE);

This way I connect to the second database, but I lose the connection to the first one. Does anyone have any ideas on how can I load the two database without having to do the following in all models constructors?

这样我连接到第二个数据库,但我失去了与第一个数据库的连接。有没有人对如何加载两个数据库而不必在所有模型构造函数中执行以下操作有任何想法?

$database1 = $this->load->database('database1', TRUE);
$database2 = $this->load->database('database2', TRUE); 

Regards,

问候,

Pedro

佩德罗

采纳答案by Christian Studer

Instead of applying the hack as mentioned by Camacho you can also set the 'pconnect'-flag in the database.php file to FALSE for all connections.

除了应用 Camacho 提到的 hack 之外,您还可以将 database.php 文件中的 'pconnect'-flag 设置为所有连接的 FALSE。

回答by mrbinky3000

There is a bug in codeigniter. Inserting one line into a class will fix the whole thing. Here is the original source: http://koorb.wordpress.com/2007/11/16/codeigniter-connect-to-multiple-databases/

codeigniter 中存在一个错误。将一行插入一个类将解决整个问题。这是原始来源:http: //koorb.wordpress.com/2007/11/16/codeigniter-connect-to-multiple-databases/

** This fix does not apply to PostgreSQL

** 此修复不适用于 PostgreSQL

Here is a copy just in case that site goes down.

这是一个副本,以防网站出现故障。

The line number has changed. Here is the bug fix from codeigniter:

行号已更改。这是来自 codeigniter 的错误修复:

开始错误修正

Description

描述

all of the database calls go to the same database (last one initialized)

所有数据库调用都转到同一个数据库(最后一个已初始化)

To fix the problem change the simple_query function in /system/database/DB_driver.php:

要解决此问题,请更改 /system/database/DB_driver.php 中的 simple_query 函数:

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

    $this->db_select(); //<-----------------  Added this line
    return $this->_execute($sql);
}

This completely fixes the problem, so you can do stuff like this in a model

这完全解决了这个问题,所以你可以在模型中做这样的事情

$this->legacy_db = $this->load->database('legacy', true);

回答by syabac

currently, codeigniter cannot connect to multiple database in persisten connection. so, you should turn of the persisten of your connections. you may can do this..

目前,codeigniter 无法在持久连接中连接到多个数据库。所以,你应该关闭你的连接的持久性。你可以这样做..

$db['default']['pconnect'] = FALSE;

$db['stats']['pconnect'] = FALSE;

回答by Pedro

I fix the problem changing the DB_driver.php on the framework.

我解决了更改框架上的 DB_driver.php 的问题。

In this function I add $this->db_select();and you never lose your connection again when working with 2 databases.

在我添加的这个函数中$this->db_select();,您在使用 2 个数据库时永远不会再次失去连接。

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

    $this->db_select();

    return $this->_execute($sql);
}

回答by Sundar

You may try modifying function CI_Session() in session.php file.

您可以尝试修改 session.php 文件中的 CI_Session() 函数。

Replace

代替

$this->CI->load->database();

with this

有了这个

$this->CI->db1 = $this->CI->load->database('default', TRUE);
$this->CI->db2 = $this->CI->load->database('db2', TRUE);

In this way, you need not load 2 dbs in all the model files but shall use them directly using objects.

这样,您不需要在所有模型文件中加载 2 个 db,而是应直接使用对象来使用它们。

$this->db1would be accessing default group db and $this->db2would be accessing db2 group db. (both db groups should have been defined in database.php)

$this->db1将访问默认组 db,$this->db2将访问 db2 组 db。(两个 db 组都应该在 database.php 中定义)



Sundar

孙达

回答by jnermano

You don't need to create separate database configurations if you only need to use a different database on the same connection. You can switch to a different database when you need to, like this:

如果您只需要在同一连接上使用不同的数据库,则无需创建单独的数据库配置。您可以在需要时切换到不同的数据库,如下所示:

$this->db->db_select($database2_name);

$this->db->db_select($database2_name);

CodeIgbiter User Guide

CodeIgbiter 用户指南