Home » Php » php – Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

php – Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

Posted by: admin April 23, 2020 Leave a comment

$db = new PDO('mysql:dbname=xnews;host=localhost;port=' . $LOCAL_DB_PORT, 
          array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES 'UTF8'")


Undefined class constant

Is it renamed?

How to&Answers:

I just had the same error (with PHP 5.2.6), and all I had to do is enable the MySQL-specific PDO driver:


In your php.ini file, you should have the following line (uncommented):

  • extension=php_pdo_mysql.dll on Windows
  • extension=php_pdo_mysql.so on Linux/Mac

Longer explanation:

  1. open php.ini in a text editor

    • e.g. its default path on Windows: C:\Program Files (x86)\PHP\v5.X\php.ini (substitute v5.x with the version you installed) or C:\Windows\php.ini, etc.
    • or on Linux: /etc/php5/apache2/php.ini (e.g. this is the path on Ubuntu) or /etc/php5/cli/php.ini, /etc/php5/cgi/php.ini, etc.
    • or you can get to know where it is like this:
      • php --ini | find /i "Loaded" in Windows command prompt OR
      • php --ini | grep "Loaded" in Linux/Mac terminal
      • using phpinfo(), and looking for the line “Loaded Configuration File”
  2. and remove the semicolon from the beginning of the following line (to uncomment it):

    • ;extension=php_pdo_mysql.dll on Windows
      • OR ;extension=php_pdo_mysql.so on Linux/Mac
    • of course, if this line doesn’t exist, you should paste it
    • so as a result the expected line would look like this in php.ini:
      • extension=php_pdo_mysql.dll on Windows
      • OR extension=php_pdo_mysql.so on Linux/Mac
  3. You may need to restart your web server.

That solved my problem.


I got the same error, on debian6, when I had not yet installed php5-mysql.

So I installed it, then restarted apache2

apt-get install php5-mysql
/etc/init.d/apache2 restart

Then the error went away.

If you have the same error on Ubuntu, instead of:

/etc/init.d/apache2 restart  


service apache2 restart


It appears to only be availabe using the mysqlnd driver.
Try replacing it with the integer it represents; 1002, if I am not mistaken.


I just tried with PHP 5.2, and that constant seems to exists :


Gives me :

int 1002

But it seems there is a bug in PHP 5.3, that causes this constant to not exists anymore — or, at least, not when the mysqlnd driver is used (and it’s the one that’s configured by default)

I suppose a temporary solution, as suggested on this bug report, could be to directly use the integer 1002 value, instead of the contant…

But note that you should go back to using the constant as soon as possible — as this makes the code easier to understand.


For Centos I was missing php-mysql library:

yum install php-mysql

service httpd restart

There is no need to enable any extension in php.ini, it is loaded by default.


For me it was missing MySQL PDO, I recompiled my PHP with the --with-pdo-mysql option, installed it and restarted apache and it was all working


You could try replacing it with 1002 or issuing your initialization query right after opening a connection.


I got this error this morning, I just did a fresh install of Fedora 14 and was trying to get my local projects back online. I was missing php-mysql, I installed it via yum and the error is now gone.


Using the int value 1002 seems to work for PHP 5.3.0:

public static function createDB() {
    $dbOptions=array(1002 => 'SET NAMES utf8',);
    return new DB($dbHost, $dbName, $dbUser, $dbPassword,$dbOptions);

function createConnexion() {
    return new PDO(


This is due to a PHP 5.3.0 bug on Windows where MYSQL_ATTR_INIT_COMMAND is not available. The PHP bug report is:


If you are experiencing this, please update your WAMP product to a version that uses PHP 5.3.1 or later version.