Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

Tags:

php

pdo

$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?

like image 756
user260019 Avatar asked Mar 11 '10 11:03

user260019


1 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:

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.

like image 170
Sk8erPeter Avatar answered Sep 28 '22 01:09

Sk8erPeter