Home » Connect Multiple Databases In CodeIgniter

Connect Multiple Databases In CodeIgniter

Today, we are going to learn how we can connect multiple databases in CodeIgniter.

Basically, In this tutorial we will see how we can handle multiple database in php when we are redirecting or doing some dynamic stuff in our application. I came to this idea because today in task I had this challenge to add multiple databases in CodeIgniter 4. Then I did this task successfully.

Connect Multiple Databases In CodeIgniter

Project Task: So, When I get assigned this task that is, “build the functionality in which we will get a particular data in request and on the basis of that particular data I have to change the database and have to fetch the data from another database. This is was the whole task.

Edit Database File For Multiple Databases

Now, If you don’t know we do have all database configuration in our database.php file under Config directory. In there we have to add a method which will return the dynamic database configuration.

//Add this method

 public function cred($host, $user, $pass, $db)
    {
        define('HOSTNAME', $host);
        define('USERNAME', $user);
        define('PASSWORD', $pass);
        define('DATABASE', $db);

        return $dynamic = [
            'DSN'      => '',
            'hostname' => HOSTNAME,
            'username' => USERNAME,
            'password' => PASSWORD,
            'database' => DATABASE,
            'DBDriver' => 'MySQLi',
            'DBPrefix' => '',
            'pConnect' => false,
            'DBDebug'  => (ENVIRONMENT !== 'production'),
            'charset'  => 'utf8',
            'DBCollat' => 'utf8_general_ci',
            'swapPre'  => '',
            'encrypt'  => false,
            'compress' => false,
            'strictOn' => false,
            'failover' => [],
            'port'     => 3306,
        ];
    }

We are done with database file and now half of the work is done. Now consider when you will get the data from request. As I have said above we will get a particular data as example: when we login in application we select state so if the state is “A” then database should be A and If “B” then database should be B.

So, For that when we redirect user then we will do this stuff. We will create dynamic connection in model.

For example: See in the code below:


    public function __construct()
    {
        $database = new Database();
        $connection = $database->cred('localhost', 'root', '', 'A');
        $this->db = Database::connect($connection, true);
    }
//get data from A database

 public function show()
    {
        return $this->db->table($this->table)->get()->getResult();
    }

Conclusion

So, In this way whenever you will make a different request to any other different model in which you want to use the different database in that you can use this method or way.

Generate Pdf Using CodeIgniter4

It worked when I have used it. So, It should work on your side also. If you do have any question you can on Twitter.

Load more posts