If you are running in localhost and if you are on Ubuntu OS, probably you may face to connect mysqli database with Codeigniter4.
CodeIgniter 4 comes with a local development server, so if you don’t have lamp/xamp, it’s okay. Still you can run the CI project with inbuilt server. You can launch it using command.
php spark serve
To use this you must have php install & Composer install in your machine.
Check first if you have composer & PHP installed or not.
php --version
composer --version
Please note composer is installing PHP separate than XAMP server. So do not confused between this. Also I noticed that the default composer not having mysqli package, you have to install it separately.
After installing composer check which php.ini file is using exactly.
php -i | grep 'php.ini'
OR
PHP --ini
OR
sudo locate php.ini
You may see output:
Configuration File (php.ini) Path => /etc/php/8.1/cli
Loaded Configuration File => /etc/php/8.1/cli/php.ini
So now you have to install that mysqli package separately.
sudo apt-get install php8.1-mysqli
You may see result like: Creating config file /etc/php/8.1/mods-available/mysqlnd.ini with new version
Means you have installed mysqli package.
Now in proper php.ini file enable mysqli service. You must see the mysqli in list of command result php -m
Stop with Php spark command terminal using ctrl+c and re enter the command php spark serve.
If you got stuck somewhere, just uninstall php & mysqli completely using command and re install composer.
php --version
mysql --version
sudo apt-get purge "^php*"
sudo apt-get purge "^mysql*"
sudo apt install composer
composer --version
If still you are not getting the mysqli connection, you can try with remote host. If you open the spark command there must be no error related to mysqli or anything other to work on clean environment.
Thanks!