我刚刚安装了Debian Lenny与Apache, MySQL和PHP,我收到一个PDOException无法找到驱动程序。

这是它所引用的特定代码行:

$dbh = new PDO('mysql:host=')。DB_HOST。”;dbname = '。Db_name, db_user, db_pass)

DB_HOST、DB_NAME、DB_USER和DB_PASS是我已经定义的常量。它在生产服务器(以及我之前的Ubuntu server设置)上运行良好。

这与我的PHP安装有关吗?

在网上搜索也没有帮助,我得到的都是专家交流和例子,但没有解决方案。


当前回答

我在使用单独的php.ini运行测试时遇到了同样的问题。我必须将这些行添加到我自己的php.ini文件中:

[PHP]
extension = mysqlnd.so
extension = pdo.so
extension = pdo_mysql.so

注意:正是这个顺序

其他回答

在php.ini中,extension=php_pdo_mysql.dll并保存。 不要忘记重新启动xampp Apache和Mysql。

对于有PHP 7.0的新版本的Ubuntu,你可以得到PHP -mysql包:

sudo apt-get install php-mysql

然后重新启动服务器:

sudo service apache2 restart

对于使用MAMP的人,

如果你有

激活扩展= php_pdo_mysql.dll 设置你的扩展所在的正确路径,(例如:extension_dir = "C:\MAMP\bin\php\php7.4.1\ext\")

...你仍然不能在phpinfo()上看到pdo_mysql扩展,然后确保你从你的web服务器的文档根调用phpinfo(),而不是从其他地方。

如果你一直在其他地方调用它,那么你可能编辑了错误的php.ini文件:

Go to the document root of your web server (the path can be found in Mamp > preferences > Web server) Create a file named info.php in this folder, open it and paste <?php phpinfo(); ?> then save the file. Open the info.php you've just created in your browser (typically, http://localhost/info.php) Now search for "Loaded Configuration File" in the page and look at the path of your php.ini file --> It's probably a different php.ini that the one you've been originally editing. So open this file and make the edits (uncomment.., etc) Restart your server Check info.php again, the extension pdo_mysql should appear.

如果没有,如果你以前像我一样改变了你的服务器的文档根路径,我建议你重新设置为“C:\MAMP\htdocs”,并把你的网站放在这里,因为这真的解决了我的问题。

希望这能帮助到一些人。

代码中的dsn显示您正在尝试连接mysql驱动程序。您的错误消息表明该驱动程序不可用。

检查你的服务器上是否安装了mysql扩展。

在Ubuntu/Debian中,你可以用以下方法检查包:

dpkg --get-selections | grep php | grep mysql

如果没有php5-mysql包,请安装。

在Ubuntu/Debian中,你可以使用:

PHP5: sudo apt-get install PHP5 -mysql PHP7: sudo apt-get安装php7.0-mysql

最后,为了让它工作,你需要重新启动你的web服务器:

Apache: sudo /etc/init.d/apache2 restart Nginx: sudo /etc/init.d/nginx restart

你检查你的php.ini(检查正确的位置与phpinfo()),如果MySQL和驱动程序安装正确?