Francisco Luz
Francisco Luz

Reputation: 2943

Fatal error: Class 'PDO' not found ( but it is installed and works on the terminal )

I got into a very odd situation.

My website hosted on a shared hosting, from out of the blue, stopped working reporting Fatal error: Class 'PDO' not found

So I created a verification file called test.php and placed it in the public_html folder.

test.php:

<?php
if (class_exists('PDO')) {
  print "PDO is installed"; 
}
else {
  print "PDO NOT installed";
}

phpinfo();
?>

So the above script checks if the PDO class is present and right after that prints php info about what is installed and enabled.

So first I did some tests on the SSH Terminal:

Then I ran the test.php on the web browser. It returned PDO NOT installed and either PDO and PDO_mysql were listed in the phpinfo() print call.

What could possibly cause such a behaviour?

UPDATE:

I have already tried to add the extension lines below into the php.ini but the issue persisted. I also made sure I had added those line into the right php.ini file by calling get_cfg_var('cfg_file_path')

extension=pdo.so

extension=pdo_mysql.so

Upvotes: 1

Views: 14093

Answers (4)

Omar Faruk
Omar Faruk

Reputation: 310

Use this after your namespace:

use PDO;

Upvotes: -2

luluboy
luluboy

Reputation: 11

Under Ubuntu 13.04, I don't know if it's the same with others os but

moving the pdo file from

/var/lib/php5/modules/apache2/enabled_by_admin

to

/var/lib/php5/modules/apache2/enabled_by_maint

make php happy on this christmas day and declare it correctly on phpinfo().

Upvotes: 1

Francisco Luz
Francisco Luz

Reputation: 2943

Ok, here is how I fixed this issue in case anyone else comes across facing the same problem.

I created a copy of the php.ini file, deleted the original, refreshed the test.php file through the web browser, copied the php.ini file back to its original place.

And that fixed it.

Upvotes: 1

Sajan Parikh
Sajan Parikh

Reputation: 4940

PHP has two separate .ini files. One for command line and one for non-command line. It's possible that you have only loaded the PDO extension in the command line .ini file. Locate your .ini files and make sure you've loaded the PDO extensions. You're looking for lines like the following.

extension=pdo.so
extension=pdo_mysql.so

If you provide us with your operating system, we can direct you exactly where to look.

Upvotes: 5

Related Questions