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

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


当前回答

在我的例子中,我使用PDO和php-cli,它工作得很好。

只有当我试图从apache连接时,我得到了“丢失的驱动程序”问题,这是我不太明白的。

一个简单的apt-get install php-mysql解决了这个问题。Ubuntu 16.04 / PHP7。学分转到选定的答案&伊万的评论)

希望能有所帮助。

其他回答

在我的情况下(Windows XP + Apache2.2 + PHP 5.4.31),我改变了PHPIniDir在httpd.conf来解决这个问题:

来自:

"C:\php5\" 

to:

"C:/php5/"

正在调用不正确的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

这招奏效了。

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 '{}' \;

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

对于那些使用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