1

please help. I got error CodeIgniter\Database\Exceptions\DatabaseException #8 when trying to connect to database. I use codeigniter 4 and when I tried to connect to database it wont connect and always got that error. I'm not sure why it happens.

this is my code when I called the table

class Komik extends BaseController
{
    protected $komikModel;
    public function __construct()
    {
        $this->komikModel = new KomikModel();
    }
 public function index()
    {
        $komik = $this->komikModel->findAll();
        dd($komik);
        return view('komik/index', $data);
    }
}

and this is my .env

 database.default.hostname = localhost
database.default.database = ci4
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi

I'm using XAMPP. please help if you know how to fix this.

Qube
  • 543
  • 3
  • 9
  • 23
  • Check if MySQLi extension is installed in your server – mail2bapi Mar 10 '21 at 16:51
  • @mail2bapi yes it already installed. but if I run it without php sprak serve and the domain are included for example `http://localhost:8080/framework/public/komik/` it works the database connected. in this case I change from XAMPP to MAMP. did you ever have the same issue? – Qube Mar 12 '21 at 01:58
  • Check MAMP has the same DB credentials as XAMPP – mail2bapi Mar 12 '21 at 14:55
  • @mail2bapi the differences in mamp it has password root and I already write it too but still same. now I have this issue again when I want to migrate the database because it says `unable to connect` – Qube Mar 15 '21 at 04:46
  • Check, `MySQLi` installed or not, type: `net start` in CMD ... search your driver, if not exist, install it. – totoprayogo1916 Mar 15 '21 at 14:05
  • Please share more details, like the full error message and your attempts to resolve the problem – Nico Haase Oct 11 '21 at 14:23

2 Answers2

2

Enable php mysqli Extension

from php.ini file in php folder

;extension=mysqli just remove ; like this extension=mysqli it's work for me. Thank You.

  • Please explain briefly why one should remove it? What does it do? – Farhan Ibn Wahid Apr 05 '21 at 20:31
  • A number of different extensions are available for PHP. The Windows PHP distribution comes with over 20 different extensions, and they're all listed (though commented out) in the php.ini file. To activate a particular extension, simply remove the semicolon at the beginning of the line and restart the server. To deactivate an extension (say, for better performance), add a semicolon to comment out the line. – Touhid Alam Apr 12 '21 at 15:43
  • 1
    don't explain in the comment, add this explanation to your answer and research how one should answer briefly in StackOverflow – Farhan Ibn Wahid Apr 12 '21 at 15:50
0

Semi column defined in php.ini file like it has been commented just like we comment any optional code. While extension of mysqli is as ;extension=mysqli it works as commented and when we remove ; it works;

But I would say now it is not required to install or need to remove ;extension=mysqli if you have latest version of xampp server,php and MVC Framework. So Everybody should install the latest version of theses.

There is only need to check if error comes Database Exception Database exception

I would personally request to configure your database correctly.

write your DB connection like this

database.default.hostname = localhost
database.default.database = your_database_name(whatever your database name is)
database.default.username = root
database.default.password =123456 (please install mysql manually by mysql 
installer there will be requirement of password (suppose there your passord is
123456, you have to keep your password here 123456.)
database.default.DBDriver = MySQLi

In your config/database.php file please make sure that your port no and mysql port no is same if not same. Kindly do the same.

one thing i will also recommend at the time of making database in mysql.

click new and put your database name after that type of collection choose

  • utf8mb4_unicode_ci

.it also affects.

flaxon
  • 928
  • 4
  • 18
  • Please share more details. If the MySQL extension was not installed, wouldn't this throw another error message? Also how is the collation related here? Usually, changing the collation does not affect whether a login is possible or not – Nico Haase Oct 11 '21 at 14:24
  • If MySQL not installed in php.ini however MySQL will run if you have latest version of server like xampp. It will not throw another error if you work with latest version of mvc framework. We can easily connect with database and can do work database related work but don't forget to manually install mysql in c/ program files and whatever password will be entered by you keep the same password in framework s database.php or.env file – Vishnu Kumar Oct 13 '21 at 11:02
  • Also match the port no in phpmyadmin and keep the same in xampp server port – Vishnu Kumar Oct 13 '21 at 11:03