duanlang1196 2013-12-29 08:11
浏览 33
已采纳

Codeigniter - 在哪里创建第二个数据库连接?

I have to use multiple database connections in my application. The scenario is:

  • I have a dm_masterdb that holds the database information and the user credentials to login to the app
  • After logging into the app, this dm_masterdb is no more needed, the database information of the logged in user is taken from the database and a connection is made according to his credentials. And now whole application operates on this newly created database connection, say userDb.

Now what I have been doing is:

I have created a the following helper that aids in connecting to the second database:

/**
 * Aids in connecting to the passed database
 * @param  string $db_name database name to which the connection is required
 * @return object          Database object for the connection made
 */
function connectDb($db_name) {
    // Get current Codeigniter instance
    $CI =& get_instance();

    try {

        $userDbConfig['hostname'] = $CI->db->hostname;
        $userDbConfig['username'] = $CI->db->username;
        $userDbConfig['password'] = $CI->db->password;
        $userDbConfig['database'] = $db_name;
        $userDbConfig['dbdriver'] = "mysqli";
        $userDbConfig['dbprefix'] = "";
        $userDbConfig['pconnect'] = FALSE;
        $userDbConfig['db_debug'] = TRUE;
        $userDbConfig['cache_on'] = FALSE;
        $userDbConfig['cachedir'] = "";
        $userDbConfig['char_set'] = "utf8";
        $userDbConfig['dbcollat'] = "utf8_general_ci";

        $userDb = $CI->load->database($userDbConfig, true);

        return $userDb;

    } catch (Exception $e) {

        $error = 'The error thrown is: ' . $e->getMessage();
        $error  .=  'Error thrown while database connection to ' . $db_name;

        show_error($error, 500);
        log_message( 'error', $error );
    }
}

This function connectDb() is called in the constructor of each and every model to create a database connection before accessing the database. For example one of my models is given below:

class Payments extends CI_Model {

    private $userDb;

    public function __construct()
    {
        parent::__construct();
        $this->userDb = connectDb($this->session->userdata('db_name'));
    }

    public function fetchChartData($period, $type)
    {
        //...
        $result = $this->userDb->query($query);
        return $result->result_array();
    }
}

Now the question is,

  1. Is this the right way that I am doing it?
  2. Is there any way that I can make it more efficient?
  3. Would it be possible that I can drop off the existing database connection to dm_masterdb and access this connection to user's database globally i.e. without having to create database connection in each and every model's constructor?
  • 写回答

3条回答 默认 最新

  • douao2000 2014-01-07 06:33
    关注

    Soon I realized that, all that making two connections did was to slow down my app.

    Finally I went with: Not create the second connection but to access the second database using it's dbname.tableName in my queries and the app was more efficient comparatively.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵