Using Multiple Databases Within CodeIgniter

前端 未结 1 536
悲&欢浪女
悲&欢浪女 2020-12-30 07:28

DISCLAIMER: I\'m new to web development

SCENARIO: I\'m building a web application that uses ion_auth to manage all of the user/administrator information (uses MySQL

相关标签:
1条回答
  • 2020-12-30 08:13

    in your database config file add as many configuration groups as the numbers of your databases:

    $db['a']['hostname'] = 'localhost';
    $db['a']['username'] = 'user';
    $db['a']['password'] = 'pw';
    $db['a']['database'] = 'db1';
    ...
    
    $db['b']['hostname'] = 'localhost';
    $db['b']['username'] = 'user';
    $db['b']['password'] = 'pw';
    $db['b']['database'] = 'db2';
    ...
    
    //set the default db
    $active_group = 'a';
    

    then on your model initialize a class variable:

    private $db_b;
    

    and, into the contructor, set it as follow

    __construct()
    {
       ...
       $this->db_b = $this->load->database('b', TRUE); 
    }
    

    now you are able to use the database b as usual:

    $this->db_b->query('YOUR QUERY');
    

    and obviously the default one as follow:

    $this->db->query('YOUR QUERY');
    
    0 讨论(0)
提交回复
热议问题