dongza6247 2012-07-24 01:26
浏览 37


I have a project in which i have to connect with user specified database. I want to implement it in a proper codeigniter's style but i dont know how can i do that codeigniter stores database credentials in a database.php file is there any way to make it dynamic. Or is there any other approach for achieving this? I have googled it but did not find anything helpful. Any help and suggestion would be appreciated.

The project is about reporting. I have a form in which i got the database login credentials and then generate the report about their database everything would be done on runtime.

  • 写回答

3条回答 默认 最新

  • duandaiqin6080 2012-07-24 01:52

    According to the guide, you can manually pass database connectivity settings via the third parameter of $this->load->model:

    $config['hostname'] = "localhost";
    $config['username'] = "myusername";
    $config['password'] = "mypassword";
    $config['database'] = "mydatabase";
    $config['dbdriver'] = "mysql";
    $config['dbprefix'] = "";
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    $this->load->model('Model_name', '', $config);
    // or as gorelative notes, to access multiple databases:
    $DB2 = $this->load->database($config, TRUE);
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
  • dongxun1978 2012-07-24 01:56

    First Off A Word of Warning

    It should go noted that this probably is not a recommended best idea, unless you limit the users allowed database names or do not allow them to select it themselves.

    If the first scenario is a must, please sanitize the data, and if you know its a defined list of db names.. provide them a list and do validation against the list.

    That being said:

    Here is your described code. I do this in my controller. And reference $db2-> in my model.

    $config['hostname'] = "localhost";
    $config['username'] = "myusername";
    $config['password'] = "mypassword";
    $config['database'] = $customUserDatabase;
    $config['dbdriver'] = "mysql";
    $config['dbprefix'] = "";
    $config['pconnect'] = FALSE;
    $config['db_debug'] = TRUE;
    $DB2 = $this->load->database($config);
    $q = $DB2->where('id', 13)->get('tablename');
    if( $q->num_rows() > 0 ){
      return $q->result();
      return false;

    Reference the part about connecting to multiple databases...

    Connecting to Multiple Databases

    If you need to connect to more than one database simultaneously you can do so as follows:

    $DB1 = $this->load->database('group_one', TRUE); $DB2 = $this->load->database('group_two', TRUE);

    Note: Change the words "group_one" and "group_two" to the specific group names you are connecting to (or you can pass the connection values as indicated above).

    By setting the second parameter to TRUE (boolean) the function will return the database object.

    When you connect this way, you will use your object name to issue commands rather than the syntax used throughout this guide. In other words, rather than issuing commands with:

    $this->db->query(); $this->db->result(); etc...

    You will instead use:

    $DB1->query(); $DB1->result(); etc...

    Live Working Code Example.

    I put a snippet of my Controller/Model using this exact same feature. I had a need to connect to multiple databases using the same credentials/configs across multiple db's.. as a result i was able to use $this->db-> to get configs.

    You can see the gist of it here:

  • douqihua6212 2016-03-18 01:42

    I know that this is an old topic, but I was looking for this information and believe that other people could need too.

    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:


    That solved to me.




  • ¥20 C语言字符串不区分大小写字典排序相关问题
  • ¥15 关于#python#的问题:我希望通过逆向技术爬取1688搜索页下滑加载的数据
  • ¥15 学习C++过程中遇到的问题
  • ¥15 关于Linux的终端里,模拟实现一个带口令保护的屏保程序遇到的输入输出的问题!(语言-c语言)
  • ¥15 学习C++过程中遇到的问题
  • ¥15 请问,这个嵌入式Linux系统怎么分析,crc检验区域在哪
  • ¥15 二分类改为多分类问题
  • ¥15 Unity微信小游戏上调用ReadPixels()方法报错
  • ¥15 如何通过求后验分布求得样本中属于两种物种其中一种的概率?
  • ¥15 q从常量变成sin函数,怎么改写python代码?