PHP Fatal error: PDOException not cleared: driver could not be found

I am trying to connect to mysql from php using PDO. However, I get this error message:

PHP Fatal error: PDOException could not be detected: driver could not be found in / home / abdullah / Documents / projects / cs 50_radio / public / test.php: 58 Stack traces: # 0 / home / abdullah / Documents / projects / cs50_radio / public /test.php(5): PDO → __ construct ('mysql: host = 127 ....')

PDO is enabled and installed. I checked phpinfo (); I can not understand 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 additional drivers or files? or I missed something in my code

+5
source share
2 answers

To use different drivers, you need to install them, on Windows you just uncomment the line in php.ini.

 extension=php_pdo_mysql.dll 

On Linux, you install the extension using the package manager:

 sudo apt install php7.1-mysql 
+7
source

I had the same problem with some incompatibility (not immediately obvious) between the versions of Apache and PHP that I 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 this script from the command line. If you do not receive the "Could not find driver" error message, this indicates incompatible versions of PHP and Apache.

+3
source

Source: https://habr.com/ru/post/1260404/


All Articles