0

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

Matt Fuller
  • 125
  • 11
  • I would run a test php page that displays `phpinfo();` to double-check that the PDO mysql extension is in fact loaded. You remember that you need to restart the http server after editing php.ini, right? – Bill Karwin Mar 24 '22 at 17:55
  • Hi @BillKarwin, thanks for this - Yes I've even restarted the entire PC. How do I do as you suggested? I'm new to PHP/backend in general. – Matt Fuller Mar 24 '22 at 18:31
  • @BillKarwin I've worked it out (literally run a page with phpinfo() lol) - there's novalue showing in the PDO drivers. Is there more I need to do other than uncomment them in the php.ini? – Matt Fuller Mar 24 '22 at 18:35
  • Does the phpinfo output tell you which php.ini it is using? A common mistake is to edit the wrong copy of php.ini. – Bill Karwin Mar 24 '22 at 19:17
  • @BillKarwin it doesn't, in Configuration File (php.ini) Path: its "no value" again – Matt Fuller Mar 24 '22 at 19:25
  • Well that would explain why your change to the php.ini file didn't fix the issue. https://documentation.mamp.info/en/MAMP-Windows/FAQ/index.html says the php.ini file is in `C:\MAMP\conf\phpX.XX` (I assume X.XX means a filename corresponding to the PHP version). Was that the file you edited? – Bill Karwin Mar 24 '22 at 19:44
  • It was, yes. Does this mean its not detecting a php ini file at all? – Matt Fuller Mar 24 '22 at 20:06
  • I've just checked the other php.ini files and they all have the correct part uncommented already too – Matt Fuller Mar 24 '22 at 20:09
  • Check https://stackoverflow.com/a/16154605/20860 and other answers on that thread. – Bill Karwin Mar 24 '22 at 20:10
  • I tried this - its for mac only and ricardo's answer doesnt apply, and the one marked as correct answer above it doesn't work as there's no value in my "Loaded Configuration File" – Matt Fuller Mar 24 '22 at 20:14
  • Okay, I'm out of ideas. I don't use Windows. – Bill Karwin Mar 24 '22 at 20:16
  • I *think* I may have fixed it. I copied the php.ini from the original location into the php 8.0.1 folder and restarted MAMP. The loaded configuration file now has a path (the new file). I'll let you know if it worked! – Matt Fuller Mar 24 '22 at 20:21
  • 1
    Yes! Thanks for all of your help @BillKarwin, very much appreciated. – Matt Fuller Mar 24 '22 at 20:29
  • You might want to summarize the solution by posting an answer, so people who find this by searching will benefit more easily than by reading this long thread of comments. This also gets this question out of the "Unanswered questions" queue. – Bill Karwin Mar 24 '22 at 20:31
  • Already have! :) – Matt Fuller Mar 24 '22 at 20:34

1 Answers1

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.

Matt Fuller
  • 125
  • 11