PHP 致命错误:未捕获的 PDOException:找不到驱动程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40827398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Fatal error: Uncaught PDOException: could not find driver
提问by A.Emad
I have been trying to connect to MySQL from PHP using PDO. However, I get this error message:
我一直在尝试使用 PDO 从 PHP 连接到 MySQL。但是,我收到此错误消息:
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....')
PHP 致命错误:未捕获的 PDOException:在 /home/abdullah/Documents/projects/cs50_radio/public/test.php:5 堆栈跟踪中找不到驱动程序:#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.
PDO 已启用并安装。我检查了 phpinfo(),但我无法弄清楚错误。
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?
我的项目文件夹是否应该包含任何其他驱动程序或文件?还是我的代码中遗漏了什么?
回答by VeeeneX
To use different drivers you need to install them. On Windows you simply uncomment a line in php.ini:
要使用不同的驱动程序,您需要安装它们。在 Windows 上,您只需取消注释 php.ini 中的一行:
extension=php_pdo_mysql.dll
On Linux you install the extension with the package manager:
在 Linux 上,您使用包管理器安装扩展:
sudo apt install php7.1-mysql
回答by flapHyman
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:
我遇到了同样的问题,这是由于我下载的 Apache 和 PHP 版本之间存在一些不兼容(不是很明显)。尝试编写一个简单的 PHP 脚本来创建一个新的 PDO 对象,例如:
<?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.
然后从命令行运行该脚本。如果您没有收到“找不到驱动程序”的错误消息,则表明您的 PHP 和 Apache 版本不兼容。
回答by Abdu
In my PHP 7.4 I didn't get php.ini. Instead, I have php.ini-developmentand php.ini-production. So, I have created a new php.ini file only and copied the configurations into it.
在我的 PHP 7.4 中,我没有得到php.ini。相反,我有php.ini-development和php.ini-production。所以,我只创建了一个新的 php.ini 文件并将配置复制到其中。
Then I have uncommented and changed the extension directory to the full installation path.
然后我取消注释并将扩展目录更改为完整安装路径。
extension_dir = "C:/php/ext"
And uncommented:
并取消注释:
extension=pdo_mysql
回答by Sebastian Rahmel
Ok, somehow I had a problem with the extension itself. It helped to reinstall the MySQL extension. Here an example for PHP 7.3:
好吧,不知何故我对扩展本身有问题。它有助于重新安装 MySQL 扩展。这是 PHP 7.3 的示例:
sudo apt purge php7.3-mysql
sudo apt install php7.3-mysql
sudo service apache2 restart