0

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Why are you showing us the phpMyAdmin config? Isn't the issue with your own code? – Dharman Jan 21 '23 at 19:35
  • Yes the issue is with my own code, I just wanted to provid all the informations I could gather but you can ignore the phpMyAdmin config if it's not usefull – Raphaël Goisque Jan 21 '23 at 19:39
  • 1
    Are you sure that your `new PDO` and `phpinfo()` are in the same file and executed in the same way? Are you maybe executing one in the browser and the other on CLI? – Dharman Jan 21 '23 at 19:41
  • Ah yes that seems to be the problem. Now when I call `phpinfo()` from the same folder as my page `home.php`, I get `PDO drivers : no value` but why and how can I fix it? Why does the extension pdo_mysql is not enabled from this path but is enabled from `http://localhost/MAMP/phpinfo.php` where I originally called it? – Raphaël Goisque Jan 21 '23 at 19:57
  • 1
    There are two separate ini files. One for Apache and one for CLI. Or maybe you have 2 PHP versions installed in MAMP – Dharman Jan 21 '23 at 19:57
  • I wish I could say you solved my problem as I realized I was not editing the good `php.ini` (C:\MAMP\conf\php7.4.1\php.ini instead of C:\MAMP\bin\php\php7.4.1\php.ini) but after applying the changes to the good file, I still get `PDO drivers : no value` in `phpinfo()` – Raphaël Goisque Jan 21 '23 at 20:49
  • Did you restart Apache? – Dharman Jan 21 '23 at 20:50
  • Yes I did. After every changes I restarted the servers in MAMP. In the good `php.ini` I've tried to specify the extension without `.dll`, I've tried both relative and absolute path to say where the extensions resides (`extension_dir = "ext"` and `extension_dir = "C:\MAMP\bin\php\php7.4.1\ext"`) but nothing seemed to work – Raphaël Goisque Jan 21 '23 at 20:57
  • Make sure you have `ini_set('display_startup_errors', '1');` enabled – Dharman Jan 21 '23 at 20:58
  • Yes it's already set to `on` – Raphaël Goisque Jan 21 '23 at 21:02
  • Could it be that MAMP or Apache doesn't have the right to access the folder containing the extensions ? – Raphaël Goisque Jan 21 '23 at 21:15

0 Answers0