drsxobip501258 2018-03-07 10:30
浏览 59
已采纳

Codeigniter中的动态数据库

I get the database information:

$data4shop['webshops'] = $this->Product_shop_model->getWebshop($businessId)->result();

I only need to insert it into the database.php. I want it to look like this, but it doesn't work:

$db['shop'] = array(
'dsn'      => '',
'hostname' => $hostname,
'username' => $username,
'password' => $password,
'database' => $database,

I don't know how to pass the variables into database.php. How can I do that?

  • 写回答

1条回答 默认 最新

  • dqellle310167 2018-03-14 13:46
    关注

    this is in the controller:

        $db_hostname = $databaseInfo['Hostname'];
        $db_username = $databaseInfo['Username'];
        $db_password = $databaseInfo['Password'];
        $db_database = $databaseInfo['Database'];
    
        $config_app = switch_db_dinamico($db_hostname, $db_username, $db_password, $db_database);
    
        $this->Product_shop_model->shop_db = $this->load->database($config_app, TRUE);
    

    This is the helper(db_dinamic_helper):

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    function switch_db_dinamico($hostname, $username, $password, $name_db)
    {
        $config_app['hostname'] = $hostname;
        $config_app['username'] = $username;
        $config_app['password'] = $password;
        $config_app['database'] = $name_db;
        $config_app['dbdriver'] = 'mysqli';
        $config_app['dbprefix'] = '';
        $config_app['pconnect'] = FALSE;
        $config_app['db_debug'] = FALSE;
        return $config_app;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 oracle中sql查询问题
  • ¥15 vue使用gojs3.0版本,在nodeDataArray中的iconSrc使用gif本地路径,展示出来后动画是静态的,不是动态的
  • ¥100 代写个MATLAB代码,有偿
  • ¥15 ansys electronics 2021 R1安装报错,错误代码2,如图
  • ¥15 Dev-c++打字不出现中文,但出现日文
  • ¥30 搭建面包板由NE555N和SN74LS90N组成的计时电路时出了问题
  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频