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?

图片转代码服务由CSDN问答提供 功能建议

我得到数据库信息:

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

我只需将其插入 为database.php。 我希望它看起来像这样,但它不起作用:

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

我不知道如何将变量传递给database.php。我该怎么做?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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;
    }
    
    点赞 打赏 评论

相关推荐