我刚刚安装了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安装

我也遇到过同样的问题。我希望这能帮助到和我有类似问题的人。

场景

OS = Windows 10  
Platform = XAMPP  
PHP Version = 7 (Multiple Version seem to have been installed in the PC)  

我在公用文件夹中创建了phpinfo.php文件,并运行phpinfo()来查找我的php.ini文件的位置。

php.ini Location = c:\xampp\php\php.ini

问题 调用c:\xampp\htdocs> php -v返回php 7.2.3,但phpinfo.php显示php 7.2.2。

解决方案 而不是打电话

php artisan migrate:install   

它给了我这个错误,我用了

c:\xampp\php\php artisan migrate:install

这招奏效了。

其他回答

Linux Mint

我在使用PhpBrew(5.5.9 / 7.0.14)并试图创建PDO连接时遇到了同样的问题。

在我尝试了这篇文章中的大部分解决方案后,我做了以下事情:

关闭PhpBrew 执行sudo apt-get install php7.0 (Linux Mint 17.2 / PHP7.0.16) -安装新的php版本

我也有同样的问题。解决方案取决于操作系统。在我的情况下,我有debian,所以解决它:

更新我的php版本从(php5到php7) 安装php-mysql和php7.0-mysql 安装php-mysql 安装php7.0-mysql 我在/etc/php/7.0/apache2/php.ini中编辑了我的php.ini 取消注释行:extension=php_pdo_mysql.dll 然后重启apache: 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”,并把你的网站放在这里,因为这真的解决了我的问题。

希望这能帮助到一些人。

对于那些使用Symfony2/3并想知道为什么会得到这个错误的人。如果您正在使用“mapping_types”,则可能会遇到此错误。原因是“mapping_types”被放在了错误的级别。例如:

doctrine:
  dbal:
    mapping_types:
        set: string

这个“mapping_types”必须放在这个级别:

doctrine:
dbal:
    #To counter the error caused by 'mapping_types'
    connections:
        default:
            server_version: %database_server_version%
            mapping_types:
                set: string

我希望这对你们有帮助

我在这里找到了解决方案:https://github.com/doctrine/DoctrineBundle/issues/327

检查模块是否可用php -m | grep pdo_mysql。

如果没有,对于PHP 7.2,可以使用sudo apt install php7.2-mysql安装相关包。

在其他PHP版本和包管理器上使用类似的命令。