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

reports:

Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

Is it renamed?

user260019
  • 533
  • 1
  • 4
  • 6
  • 1
    Seems to be a bug of PHP5.3... – user260019 Mar 11 '10 at 11:06
  • It's a bug: http://bugs.php.net/47224 – Davide Gualano Mar 11 '10 at 11:14
  • Any time you upgrade PHP, make sure you backup your php.ini. On windows, you might have a new PHP directory. If it is just a minor version / patch to PHP, copy over your previous php.ini. If you are changing versions, compare the old and new php.ini to see what extensions and settings you might want to keep / transfer to the new php.ini file. – Frank Forte Oct 18 '16 at 19:07

14 Answers14

68

A note from the future: most likely you don't need this parameter at all, just like in the opening post. Instead of setting charset using this awkward option, it should be done in DSN:

$db = new PDO('mysql:dbname=xnews;host=localhost;charset=utf8mb4;port=3306', 
      $LOCAL_DB_USER, 
      $LOCAL_DB_PASS, 
  );

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

TL;DR

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
  • Works for PHP 7 as well. – Alexander Nov 17 '16 at 09:08
  • 4
    Hi, I tried this, but now I get the warning `PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20151012/php_pdo_mysql.so' - /usr/lib/php/20151012/php_pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0` – George Mar 24 '17 at 06:11
  • Thank you! This work for me on Windows IIS 7 and PHP 7.1.21. – user752746 Sep 14 '18 at 15:53
  • I also had to disable php7.2 and enable php7.3 `a2dismod php7.2; a2enmod php7.3; systemctl restart apache2` – phoxd Nov 29 '19 at 20:13
  • In my case, while installing opencart v4 on windows10 and PHP8, this problem solved by uncommenting: extension=pdo_mysql in c:\php\php.ini – Isabella Dec 21 '22 at 11:37
34

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  

Type:

service apache2 restart
Francesco D.M.
  • 2,129
  • 20
  • 27
Sverre
  • 1,318
  • 13
  • 21
7

For anyone coming along later, like I did, and seeing this...

I was receiving the same error because I did not have the extension "enabled" in my php.ini file. I would imagine you might also get this error if you have recently upgraded your php version and did not properly update your php.ini file.

If you are receiving this error shortly after upgrading your php version, the info below might help you out:

PHP 7.4 slightly changed its syntax in the php.ini file. Now, to enable the mysql pdo, make sure extension=pdo_mysql is uncommented in your php.ini file. (line 931 in the default php.ini setup)

The line used to be:

extension=php_pdo_mysql.dll on Windows

extension=php_pdo_mysql.so on Linux/Mac

as Sk8erPeter pointed out. but the .dll and .so endings are to be deprecated and so it is best practice to begin getting rid of those endings and using just extension=<ext>

The below is pulled from the default php.ini-production file from the php 7.4 zip download under "Dynamic Extensions":

Note : The syntax used in previous PHP versions ('extension=.so' and 'extension='php_.dll') is supported for legacy reasons and may be deprecated in a future PHP major version. So, when it is possible, please move to the new (extension=<ext>) syntax.

Community
  • 1
  • 1
IDaedalusI
  • 81
  • 1
  • 4
  • Can you explain how this is related to the given question about any missing constant? – Nico Haase Apr 30 '20 at 21:26
  • 1
    Sorry, you're right, I should have given more context. I was getting the same error as above because I did not have the extension enabled. I'll be sure to edit to clarify. – IDaedalusI May 01 '20 at 15:59
5

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

Atli
  • 7,855
  • 2
  • 30
  • 43
5

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.

Aris
  • 4,643
  • 1
  • 41
  • 38
4

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

var_dump(PDO::MYSQL_ATTR_INIT_COMMAND);

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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

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

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Reza S
  • 9,480
  • 3
  • 54
  • 84
1

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

Andrew
  • 1,203
  • 8
  • 12
1

While this question is fairly old, I just got here with a similar issue. These days with PHP namespaces, it seems you need \PDO instead of just PDO. For example:

$options = array(\PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES 'UTF8'");

instead of:

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND =>  "SET NAMES 'UTF8'");
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
0

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:

http://bugs.php.net/bug.php?id=47224

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

Siraj Khan
  • 2,328
  • 17
  • 18
0

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.

Jeff Busby
  • 2,001
  • 17
  • 12
0

On fedora with PHP 8.0.16:

sudo dnf install php-mysqlnd.x86_64
Néstor Waldyd
  • 924
  • 7
  • 6
0

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

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

function createConnexion() {
    return new PDO(
        "mysql:host=$this->dbHost;dbname=$this->dbName",
        $this->dbUser,
        $this->dbPassword,
        $this->dbOptions); 
}
JDelage
  • 13,036
  • 23
  • 78
  • 112
  • I tried this approach using PHP 5.3.0 (cli) and ended up with a "Fatal error: Uncaught exception 'PDOException' with message 'could not find driver'" – John Erck Jul 12 '12 at 20:06
-1

If you are on windows using PHP8 just uncomment extension=pdo_mysql in c:\php\php.ini

Isabella
  • 137
  • 12