Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Multiple data base connections in codeigniter

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 36
    Comment on it

    We can create multiple database connections easily by providing second database information in application/config/database.php.

    Normally we already set the default database group which is by default. Like this

      $db['default']['hostname'] = "localhost";
        $db['default']['username'] = "root";
        $db['default']['password'] = "";
        $db['default']['database'] = "databasename";
    

    we can create another database connections by creating a new array name as seconddb just exactly a copy of default group array but replace seconddb instead of default in case of second db.

    $db['seconddb']['hostname'] = "localhost";
    $db['seconddb']['username'] = "root";
    $db['seconddb']['password'] = "";
    $db['seconddb']['database'] = "seconddatabasename";
    

    Next is we need to send the connection to another variable inside model for second database.

    function mytestmodel_method()
    {
      $otherdb = $this->load->database('otherdb', TRUE);
    
      $query = $otherdb->select('name')->get('person');
      var_dump($query);
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: