12

I have been trying to connect to MySQL from PHP using PDO. However, I get this error message:

PHP Fatal error: Uncaught PDOException: could not find driver in /home/abdullah/Documents/projects/cs50_radio/public/test.php:5 Stack trace: #0 /home/abdullah/Documents/projects/cs50_radio/public/test.php(5): PDO->__construct('mysql:host=127....')

PDO is enabled and installed. I checked phpinfo(), but I can't figure out the error.

Here is my code used to connect:

<?php
    $user = "root";
    $pass = "root";

    $dbh = new PDO("mysql:host=127.0.0.1;dbname=radio;port=3306", $user, $pass);
    //$dbh->query('INSERT INTO users (name) VALUES ("abdullah")');
    $dbh = null;
?>

Should my project folder contain any additional drivers or files? Or am I missing something in my code?

4
  • new PDO("mysql:host=127.0.0.1;dbname=radio", $user, $pass); Commented Nov 27, 2016 at 9:21
  • Hi, are you sure the mysql driver is loaded ? Can you check in your php.ini ? The driver line must be uncommented Commented Nov 27, 2016 at 9:24
  • which line is that? Commented Nov 27, 2016 at 9:31
  • 1
    You should have extension=php_pdo_mysql.dll and not ;extension=php_pdo_mysql.dll (no semi colon) Commented Nov 27, 2016 at 9:38

4 Answers 4

18

To use different drivers you need to install them.

On Windows you simply uncomment a line in php.ini:

extension=php_pdo_mysql.dll

On Debian or similar Linux distribution you can just install the extension with the package manager:

sudo apt update
sudo apt install php7.1-mysql

Remember to install the correct version.

Sign up to request clarification or add additional context in comments.

1 Comment

Reading package lists... Done Building dependency tree Reading state information... Done php5-mysql is already the newest version. php5-mysql set to manually installed. 0 upgraded, 0 newly installed, 0 to remove and 95 not upgraded.
6

I had the same problem, resulting from some incompatibility (not immediately evident) between the Apache and PHP versions I had downloaded. Try writing a toy PHP script that simply creates a new PDO object, - something like:

<?php

$dbname   = 'mydb';
$username = 'myuser';
$password = 'mypassword';
try {
    $pdo = new \PDO("mysql:host=localhost;dbname=$dbname", $username,  $password);
} catch (Exception $e) {
    print $e->getMessage() . "\n";
}
print "OK\n";

Then run that script from the command line. If you don't get the 'could not find driver' error message, then that points to an incompatibility between your PHP and Apache versions.

Comments

6

In my PHP 7.4 I didn't get php.ini. Instead, I have php.ini-development and php.ini-production. So, I have created a new php.ini file only and copied the configurations into it.

Then I have uncommented and changed the extension directory to the full installation path.

 extension_dir = "C:/php/ext"

And uncommented:

 extension=pdo_mysql

Finally, restart the server.

1 Comment

I just wanted to point out here that it is important to restart your PHP server after uncommenting extension=pdp_mysql so as to allow the changes to take effect.
0

Ok, somehow I had a problem with the extension itself. It helped to reinstall the MySQL extension. Here an example for PHP 7.3:

sudo apt purge php7.3-mysql
sudo apt install php7.3-mysql
sudo service apache2 restart

1 Comment

The third line help me after doing sudo apt install php7.1-mysql

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.