php PDOException“找不到驱动程序”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2852748/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:54:52  来源:igfitidea点击:

PDOException “could not find driver”

phpmysqlpdolamp

提问by Mike Moore

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a PDOException could not find driver.

我刚刚用 Apache、MySQL 和 PHP 安装了 Debian Lenny,我收到了 PDOException could not find driver

This is the specific line of code it is referring to:

这是它所指的特定代码行:

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)

$dbh = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS)

DB_HOST, DB_NAME, DB_USER, and DB_PASSare constants that I have defined. It works fine on the production server (and on my previous Ubuntu Server setup).

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

Is this something to do with my PHP installation?

这与我的 PHP 安装有关吗?

Searching the internet has not helped, all I get is experts-exchange and examples, but no solutions.

搜索互联网没有帮助,我得到的只是专家交流和示例,但没有解决方案。

采纳答案by ZZ Coder

You need to have a module called pdo_mysql. Looking for following in phpinfo(),

您需要有一个名为 pdo_mysql 的模块。在 phpinfo() 中寻找以下内容,

pdo_mysql

PDO Driver for MySQL, client library version => 5.1.44

回答by ghbarratt

The dsn in your code reveals you are trying to connect with the mysql driver. Your error message indicates that this driver is unavailable.

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

Check that you have the mysql extension installed on your server.

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

In Ubuntu/Debian you check for the package with:

在 Ubuntu/Debian 中,您可以使用以下命令检查软件包:

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

Install the php5-mysql package if you do not have it.

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

In Ubuntu/Debian you can use:

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

  • PHP5: sudo apt-get install php5-mysql
  • PHP7: sudo apt-get install php7.0-mysql
  • PHP5: sudo apt-get install php5-mysql
  • PHP7: sudo apt-get install php7.0-mysql

Lastly, to get it working, you will need to restart your web-server:

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

  • Apache: sudo /etc/init.d/apache2 restart
  • Nginx: sudo /etc/init.d/nginx restart
  • 阿帕奇: sudo /etc/init.d/apache2 restart
  • nginx: sudo /etc/init.d/nginx restart

回答by Sudipta Chatterjee

Update: newer versions should use php-sqlite3package instead of php5-sqlite. So use this, if you are using a recent ubuntu version:

更新:较新的版本应该使用php-sqlite3package 而不是php5-sqlite. 因此,如果您使用的是最近的 ubuntu 版本,请使用它:

sudo apt-get install sqlite php-sqlite3

Original answer to question is here:

问题的原始答案在这里:

sudo apt-get install sqlite php5-sqlite
sudo /etc/init.d/apache2 restart

If your phpinfo() is not showing the pdo_sqlite line (in my case, on my Ubuntu Server), you just need to run the lines above and then you'll be good to go.

如果您的 phpinfo() 没有显示 pdo_sqlite 行(在我的情况下,在我的 Ubuntu 服务器上),您只需要运行上面的行,然后就可以了。

回答by ThomasAFink

For newer versions of Ubuntu that have PHP 7.0 you can get the php-mysqlpackage:

对于具有 PHP 7.0 的较新版本的 Ubuntu,您可以获得以下php-mysql软件包:

sudo apt-get install php-mysql

Then restart your server:

然后重启你的服务器:

sudo service apache2 restart

回答by onlyme

I had the same issue. The solution depends on OS. In my case, i have debian, so to solve it:

我遇到过同样的问题。解决方案取决于操作系统。就我而言,我有 debian,所以要解决它:

  • Updated my php version from (php5 to php7)
  • Install php-mysql and php7.0-mysql

    apt-get install php-mysql
    apt-get install php7.0-mysql
    
  • I edited my php.inilocate at /etc/php/7.0/apache2/php.ini

    uncomment the line : extension=php_pdo_mysql.dll
    
  • Then restart apache:

    service apache2 restart
    
  • 更新了我的 php 版本(从php5 到 php7
  • 安装 php-mysql 和 php7.0-mysql

    apt-get install php-mysql
    apt-get install php7.0-mysql
    
  • php.ini在 /etc/php/7.0/apache2/php.ini编辑了我的位置

    uncomment the line : extension=php_pdo_mysql.dll
    
  • 然后重启apache:

    service apache2 restart
    

This solves my problem

这解决了我的问题

回答by berdzi

On my Windows machine, I had to give the absolute path to the extension dir in my php.ini:

在我的 Windows 机器上,我必须在我的 php.ini 中给出扩展目录的绝对路径:

extension_dir = "c:\php5\ext"

extension_dir = "c:\php5\ext"

回答by Dmitry Sobolev

On Ubuntu just execute

在 Ubuntu 上执行

sudo apt-get install php5-mysql

回答by Moses Nandwa

sudo apt-get install php-mysql 

worked well on ubuntu and php 7

在 ubuntu 和 php 7 上运行良好

回答by Pratik

Check if the module is available with php -m | grep pdo_mysql.

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

If not, for PHP 7.2, you can install relevant package with sudo apt install php7.2-mysql.

如果没有,对于 PHP 7.2,您可以使用sudo apt install php7.2-mysql.

Use similar command on other PHP versions and package managers.

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

回答by Paul Foster

When adding these into your php.ini ensure the php_pdo.dll reference is first before the db drivers dlls otherwise this will also cause this error message too. Add them like this:

将这些添加到 php.ini 时,请确保 php_pdo.dll 引用在 db 驱动程序 dll 之前是第一个,否则这也会导致此错误消息。像这样添加它们:

[PHP_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll