66
PHP Fatal error:  Class 'PDO' not found in /home/bd/public_html/app/webroot/Cake/Model/Datasource/Database/Mysql.php on line 177

PHP INFO:

PDO

PDO support => enabled
PDO drivers => sqlite, sqlite2, mysql

pdo_mysql

PDO Driver for MySQL => enabled
Client API version => 5.5.24

Directive => Local Value => Master Value
pdo_mysql.default_socket => /var/lib/mysql/mysql.sock => /var/lib/mysql/mysql.sock

pdo_sqlite

PDO Driver for SQLite 3.x => enabled
SQLite Library => 3.7.7.1

PHP INI:

extension=pdo.so
extension=pdo_sqlite.so
extension=sqlite.so
extension=pdo_mysql.so

CODE:

/**
 * Check whether the MySQL extension is installed/loaded
 *
 * @return boolean
 */
        public function enabled() {
                return in_array('mysql', PDO::getAvailableDrivers());
        }

Ideas as to why I'm getting this error?

PHP 5.3.15 CloudLinux/CentOS 6 CPanel

Amanada Smith
  • 1,893
  • 9
  • 28
  • 42

15 Answers15

76

try

 yum install php-pdo
 yum install php-pdo_mysql

 service httpd restart
sj59
  • 2,072
  • 3
  • 22
  • 23
Nam Nguyen
  • 2,438
  • 21
  • 18
75

Try adding use PDO; after your namespace or just before your class or at the top of your PHP file.

Jo Smo
  • 6,923
  • 9
  • 47
  • 67
  • 6
    That did it for me... I was inside a namespaced class, and simply adding a ` \ ` in front of `PDO` as suggested elsewhere didn't work - but this did :) – erlingormar Jan 25 '15 at 13:29
  • Question didn't mention he was using namespace. if some one use namespace for all other customer classes this should be the acceptable answer. Because somewhare in the code he missed `use PDO` Otherwise need to install correct PDO version libraries into their webserver. – Elshan Oct 27 '19 at 15:09
  • This helped me out a bunch. Thank you. This works too if anybody wonders because PDO is a global class within PHP. – wowzuzz Oct 02 '20 at 20:07
22

This can also happen if there is a php.ini file in the web app's current working directory. If one has been placed there to change certain settings, it will override the global one.

To avoid this problem, don't use a php.ini file to change settings; instead you can:

  • Specify settings in the vhost declaration
  • Use an .htaccess file with php_flag (see here)
  • Use an .user.ini file (see here)
Simon East
  • 55,742
  • 17
  • 139
  • 133
Alastair Irvine
  • 1,166
  • 12
  • 16
15

Ensure they are being called in the php.ini file

If the PDO is displayed in the list of currently installed php modules, you will want to check the php.ini file in the relevant folder to ensure they are being called. Somewhere in the php.ini file you should see the following:

extension=pdo.so
extension=pdo_sqlite.so
extension=pdo_mysql.so
extension=sqlite.so

If they are not present, simply add the lines above to the bottom of the php.ini file and save it.

Bruno Ribeiro
  • 1,280
  • 16
  • 21
10

What is the full source of the file Mysql.php. Based on the output of the php info list, it sounds like you may be trying to reference a global class from within a namespace.

If the file Mysql.php has a statement "namespace " in it, use \PDO in place of PDO - this will tell PHP to look for a global class, rather than looking in the local namespace.

Stephen
  • 18,597
  • 4
  • 32
  • 33
  • well no namespaces there, I'm stumped again. – Stephen Aug 06 '12 at 12:58
  • I'm just wondering if maybe this is a poorly coded script. I had to actually move the Cake framework into the webroot directory just to even get this far ;/ script is: http://www.freereviewscript.com/p/requirements – Amanada Smith Aug 06 '12 at 13:41
3

I had the same problem on GoDaddy. I added the extension=pdo.so to php.ini, still didn't work. And then only one thing came to my mind: Permissions

Before uploading the file, kill all PHP processes(cPanel->PHP Processes).

The problem was that with the file permissions, it was set to 0644 and was not executable . You need to set the file permission at least 0755.

Permissions

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77
3

you can just find-out loaded config file by executing below command,

 php -i | grep 'php.ini'

Then add below lines to correct php.ini file

extension=pdo.so
extension=pdo_sqlite.so
extension=pdo_mysql.so
extension=sqlite.so

Then restart web server,

service httpd restart
Hasitha
  • 738
  • 8
  • 16
3

Its a Little Late but I found the same problem and i fixed it by a "\" in front of PDO

public function enabled() {
    return in_array('mysql', \PDO::getAvailableDrivers());
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Wells
  • 142
  • 1
  • 13
  • you can use multi_query for what you described (https://stackoverflow.com/a/22469722/2943403), but you shouldn't. `multi_query()` does not allow parameterization. Use a single prepared statement with looped executions and grab the new ids as you go. – mickmackusa Dec 03 '20 at 11:58
  • Adapt this: https://stackoverflow.com/a/25939954/2943403 to get the new ids let me know when you've read these comments so that I can delete them. – mickmackusa Dec 03 '20 at 12:06
1

This error is caused by PDO not being available to PHP.

If you are getting the error on the command line, or not via the same interface your website uses for PHP, you are potentially invoking a different version of PHP, or utlising a different php.ini configuration file when checking phpinfo().

Ensure PDO is loaded, and the PDO drivers for your database are also loaded.

Predominant
  • 1,460
  • 12
  • 24
  • It is loaded as mentioned in comments above. – Amanada Smith Aug 06 '12 at 11:57
  • 2
    +10 That's so ridiculous to vote down...it's exactly what happens with me..after upgrading PHP to 7.1 PDO not available...some downvoters really weird. is this kind of a hobby in the stack? – wpcoder Oct 29 '17 at 09:56
  • 1
    I think a lot of people don't realize this site is a resource for everyone, not just OP. Not every answer will be _specifically_ of use to OP, but that doesn't mean they will be of no use to others. This one was the clue I needed, for instance. Upvoted. Cheers. – Phil Dec 18 '18 at 10:57
1

For Fedora 33 you can install as follows:

Install

dnf install php-pdo
dnf install php-pdo_mysql

Restart PHP

systemctl restart php-fpm.service
Aris
  • 4,643
  • 1
  • 41
  • 38
0

I solved it with library PHP_PDO , because my hosting provider didn't accept my requirement for installation of PDO driver to apache server.

Pavel Hájek
  • 176
  • 1
  • 8
-1

If you run php with php-fpm module,do not forget to run command systemctl restart php-fpm!That will reload php-fpm module.

-1

I had to run the following on AWS EC2 Linux instance (PHP Version 7.3):

sudo yum install php73-php-pdo php73-php-mysqlnd
Justin
  • 901
  • 8
  • 11
-1

If anyone getting this error in cPanel, please check the PHP version type in your cPanel. Change it, alt-php to ea-php. This setting worked for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135
-1

After a long time, I finally solved it. check your folder in Cpanel to see if there is a php.ini file. if yes delete it since Cpanel will be using its own php.ini