'Fatal error: Uncaught PDOException: could not find driver - windows MAMP php. The PHP.ini file has the correct part uncommented

I have the below error -

Fatal error: Uncaught PDOException: could not find driver 

When trying to connect via PDO.

<?php


$host = '127.0.0.1';
$db   = 'mytodo';
$user = 'root';
$pass = 'root';
$charset = 'utf8mb4';

$options = [
    \PDO::ATTR_ERRMODE            => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
    \PDO::ATTR_EMULATE_PREPARES   => false,
];

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
try {
     $pdo = new \PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
     throw new \PDOException($e->getMessage(), (int)$e->getCode());
}

require ('index.view.php');

I've gone through countless instances of the same issue and all were resolved by uncommenting the "extension=php_pdo_mysql.dll" in php.ini, but it has made no difference for me.

I'm using Windows 10, MAMP and PHP 8.0.1

phpinfo() is showing no drivers under PDO

PDO drivers

despite being uncommented in php.ini

php.ini Any help would be appreciated



Solution 1:[1]

I'm not entirely sure if this is the correct way to fix this issue, but it worked so i'm guessing so?

Turns out the location listed in the MAMP documentation isn't the one I was looking for C:\MAMP\conf\phpX.XX - It was in fact C:\MAMP\bin\php\phpX.X.X. The issue I had after realising this is that there WASN'T a php.ini file in here, which is the reason I assumed it to be the other location. I then copied the .ini from the C:\MAMP\conf\phpX.XX location, and restarted MAMP. Boom.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Matt Fuller