Php – Fatal error: Class ‘PDO’ not found ( but it is installed and works on the terminal )

apachepdoPHP

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:

  • Before running the test.php I checked for the installed modules: $ php -m and both PDO and PDO_mysql were there;
  • Then I ran $ cd public_html $ php -f test.php. It returned that the PDO class was installed and both PDO and PDO_mysql were listed from the phpinfo() call.

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

Best Answer

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.