PDOException “could not find driver”

2018-12-31 04:53发布

I have just installed Debian Lenny with Apache, MySQL, and PHP and I am receiving a 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)

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

Is this something to do with my PHP installation?

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

标签: php mysql pdo lamp
30条回答
旧人旧事旧时光
2楼-- · 2018-12-31 05:30

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_PDO]
extension=php_pdo.dll
[PHP_PDO_MYSQL]
extension=php_pdo_mysql.dll
查看更多
冷夜・残月
3楼-- · 2018-12-31 05:30

I had the same problem during running tests with separate php.ini. I had to add these lines to my own php.ini file:

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

Notice: Exactly in this order

查看更多
深知你不懂我心
4楼-- · 2018-12-31 05:31

If you are using sqlite for testing you will need php sqlite pdo drive. You can install them as below.

For Ubuntu 14.04

sudo apt-get install php5-sqlite
sudo service apache2 restart

In ubuntu 16.04 there is no php5-sqlite

sudo apt-get install php7.0-sqlite
sudo service apache2 restart
查看更多
旧时光的记忆
5楼-- · 2018-12-31 05:32

Check if extension_dir in php configuration file set correctly. Try to comment/uncomment some extensions and see if it's reflected on phpinfo(). If it doesn't then either php config file cannot be loaded (wrong location) extension_dir is commented or set to the wrong location.

查看更多
深知你不懂我心
6楼-- · 2018-12-31 05:32

The problem is a missing php to mysql library. In CentOs i fixed it by running # yum install php-mysql and then restarting apache with # /bin/systemctl restart httpd.service Note that the naming is slightly different from debian/ubuntu based distros, php->php5 and httpd->apache2.

查看更多
荒废的爱情
7楼-- · 2018-12-31 05:33

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

extension_dir = "c:\php5\ext"

查看更多
登录 后发表回答