I’ve been struggling trying to connect PHP to MySQL using the PDO() constructor since yesterday, and despite all the answers I’ve found on this problem, none of them worked.
In phpMyAdmin I have a database called ‘database_test’ and I’m trying to connect to it.
I’ve tried with ‘localhost’ and with ‘localhost:3306’:
<?php $mysqlConnection = new PDO('mysql:host=localhost;dbname=database_test;charset=utf8','root','root');?>
Both returning the same error when accessing http://localhost:8080/home.php :
Fatal error: Uncaught PDOException: could not find driver in C:\Users...\home.php:3 Stack trace: #0 C:\Users...\home.php(3): PDO->__construct('mysql:host=loca...', 'root', 'root') #1 {main} thrown in C:\Users...\home.php on line 3
Yet, the module pdo_mysql is enabled
Mamp says : “To connect to the MySQL server from your own scripts use the following connection parameters”:
- Host : localhost
- Port : 3306
- User : root
- Password : root
Which I did.
Configuration
- System : Windows 11
- MAMP 4.2.0
- Apache 2.2.31
- MySQL 5.7.24
- PHP 7.4.1
- PhpMyAdmin 4.8.3
In phpinfo():
mysqlnd | enabled |
---|---|
API extensions | mysqli,pdo_mysql |
PDO support | enabled |
---|---|
PDO drivers | sqlite, mysql |
PDO Driver for MySQL | enabled |
---|---|
Client API version | mysqlnd 7.4.1 |
Pdo_sqlite | enabled |
---|---|
SQLite Library | 3.30.1 |
In my php.ini (located here : C:\MAMP\conf\php7.4.1\php.ini), among the extensions that are uncommented I have :
extension=php_mysqli.dll
extension=php_pdo_sqlite.dll
extension=php_sqlite3.dll
extension=php_pdo_mysql.dll
; ...
; Directory in which the loadable extensions (modules) reside.
extension_dir = "C:\MAMP\bin\php\php7.4.1\ext\"
; ps : I've checked, they're all here