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:40

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

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.

查看更多
高级女魔头
3楼-- · 2018-12-31 05:41

Incorrect installation of PHP was being called

I was experiencing the same problem. And I hope this would help someone who is having a similar issue as me.

Scenario

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

I created phpinfo.php file in the public folder and run the phpinfo() to look for the location of my php.ini file.

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

Problem
Calling c:\xampp\htdocs> php -v returned PHP 7.2.3 but phpinfo.php showed PHP 7.2.2.

Solution
Instead of calling

php artisan migrate:install   

which gave me this error, I used

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

and it worked.

查看更多
ら面具成の殇う
4楼-- · 2018-12-31 05:44

Did you check your php.ini (check for the correct location with phpinfo()) if MySQL and the driver is installed correctly?

查看更多
忆尘夕之涩
5楼-- · 2018-12-31 05:45

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

  • 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.ini locate at /etc/php/7.0/apache2/php.ini

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

    service apache2 restart
    

This solves my problem

查看更多
冷夜・残月
6楼-- · 2018-12-31 05:45

I faced the same issue after I removed the php5 package (that includes all the drivers as well) in order to install php7 package. I actually installed php7 package without a mysql module.

I managed to solve it by typing in the terminal:

1) $ apt-cache search php7 which lists all the modules, looking through the modules I found,

php7.0-mysql - MySQL module for PHP

2) $ sudo apt-get install php7.0-mysql

That's it. It worked for me in my linux system.

(use the appropriate php version, yours could be php5)

查看更多
长期被迫恋爱
7楼-- · 2018-12-31 05:47

for Windows 8.1/10 in :\\php.ini file you should uncomment line "extension=pdo_mysql"

查看更多
登录 后发表回答