我刚刚安装了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安装有关吗?

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


当前回答

还有一件事需要确认,因为有些人是从互联网上复制/粘贴示例代码来开始的。确保你在这里输入了MySQL:

... $dbh = new PDO ("mysql: ...  

在一些例子中可以看到

$dbh = new PDO ("dblib ...

其他回答

当添加这些到你的php.ini时,确保php_pdo.dll引用在db驱动dll之前,否则这也会导致这个错误消息。像这样加起来:

[PHP_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
PHP Fatal error:  Uncaught PDOException: could not find driver

我挣扎着,挣扎着“apt安装php-mysql php7toInfinity和不要忘记sqlite- whatever -ever's”,只是无法摆脱这个错误消息,直到我回到基础,重置文件权限在网站上的问题。

这3个命令重置文件和文件夹的权限在网站上,让它再次工作。

cd /var/www/web-site-name.com/web/

# find (sub) directories and change permissions
find . -type d -exec chmod 755 {} \;

# find files and change permissions
find . -type f -exec chmod 664 '{}' \;

对于使用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”,并把你的网站放在这里,因为这真的解决了我的问题。

希望这能帮助到一些人。

这个答案是mysql数据库和谁正在使用Xammp

我尝试了很多来自StackOverflow的代码,但主要问题是,我们必须检查这两个文件xammp php.ini文件和php文件夹php.ini文件,并取消注释这个扩展名“extension=pdo_mysql”在这两个。ini注释一个看起来像这样

;extension=pdo_mysql

在重新启动xammp并在终端中运行这些命令之后,只需从开头删除“;”分号

php artisan migrate:fresh --seed

这个命令将刷新你的迁移,也做了播种

关闭上次运行服务命令并再次运行它

php artisan serve

现在它为你工作了。

如果有任何错误,那么任何高级都可以更新这个答案

我的解决方案是在安装php时,没有正确配置环境变量和php.ini文件。做这两个修正未注释的de line extension = pdo_mysql。所以工作