Skip to content

PHP code snippet – How to use more than one database inigniter?

This is default database :

$db['default'] = array(
    'database' => 'mydatabase',
Add another database at the bottom of database.php file

$db['second'] = array(
    'database' => 'mysecond',

In autoload.php config file

$autoload['libraries'] = array('database', 'email', 'session');

The default database is worked fine by autoload the database library but second database load and connect by using constructor in model and controller...

    class Seconddb_model extends CI_Model {
        function __construct(){
            //load our second db and put in $db2
            $this->db2 = $this->load->database('second', TRUE);

        public function getsecondUsers(){
            $query = $this->db2->get('members');
            return $query->result(); 

I hope it will help you.
Stay Home Stay Safe
See also  Java code snippet - How to pass string between activities android?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.