How to connect database in codeigniter 4

Deepak Tailor Image
Deepak Tailor - Oct 06 2020
How to connect database in codeigniter 4

The database.php file is given to connect the database in the codeigniter, in which you can set the details of your database. This file is named app / config / database.php. This file contains setting the username, password, database name etc. of the database.

public $default = [
	'DSN'      => '',
	'hostname' => 'localhost',
	'username' => 'root', // your database username
	'password' => '', // your database password
	'database' => 'database_name', // your database name
	'DBDriver' => 'MySQLi',
	'DBPrefix' => '',
	'pConnect' => TRUE,
	'DBDebug'  => TRUE,
	'cacheOn'  => FALSE,
	'cacheDir' => '',
	'charset'  => 'utf8',
	'DBCollat' => 'utf8_general_ci',
	'swapPre'  => '',
	'encrypt'  => FALSE,
	'compress' => FALSE,
	'strictOn' => FALSE,
	'failover' => [],
Connect database with .env file

We can also connect the database to the .env file. same details have to be added to this as well.

database.default.hostname = localhost
database.default.database = ci4_article // your database name
database.default.username = root // database username
database.default.password =      // database password
database.default.DBDriver = MySQLi
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
You can contact him at

Do you want to make website?

Message on WhatsApp, Plan your business idea with your website.

+91 7742307462