0

I uncommented the lines extension=php_pdo_mysql.dll extension=php_pdo.dll in my php.ini file but I still got the error Fatal error: Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' None of the previous answers worked for me what should I do ?

 $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'); 
try 
{ $cnx = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password,$options); 
} 
catch(PDOException $ex) 
{  die("Failed to connect to the database: " . $ex->getMessage()); 
} 
student23
  • 68
  • 1
  • 2
  • 12
  • 1
    did you restart everything after making those changes? If not, then do. Those changes won't take effect until you restarted all services. – Funk Forty Niner Jun 06 '16 at 13:43
  • 2
    See also http://php.net/manual/en/ref.pdo-mysql.connection.php *"Setting the connection character set to UTF-8 prior to PHP 5.3.6"* - and http://php.net/manual/en/ref.pdo-mysql.php and this Q&A http://stackoverflow.com/questions/2424343/undefined-class-constant-mysql-attr-init-command-with-pdo and http://stackoverflow.com/questions/17476106/undefined-class-constant-mysql-attr-init-command-in-mamp-using-php-5-4-4 and http://stackoverflow.com/questions/23059697/undefined-class-constant-mysql-attr-init-command-in-undefined-class-constant – Funk Forty Niner Jun 06 '16 at 13:49
  • Are u using Microsoft-IIS? If yes? Did you reboot your IIS after you uncommented the extension=php_pdo_mysql.dll – Paules Jun 06 '16 at 13:51
  • @Fred-ii- yes I did still wont work, and I actually found it uncommented from the start. – student23 Jun 06 '16 at 13:56
  • have a look at the links I left you above. – Funk Forty Niner Jun 06 '16 at 13:56
  • did you set the extension_dir in php.ini? it should like: extension_dir = ".\ext" – Paules Jun 06 '16 at 13:58
  • @Paules I Changed `;extension_dir = "./"` to `extension_dir = "./ext"` and restart everything but I still got the same error – student23 Jun 06 '16 at 14:06

1 Answers1

0

Try this one once..!

try 
{ 
$cnx = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
} 
catch(PDOException $ex) 
{  die("Failed to connect to the database: " . $ex->getMessage()); 
} 

// need more

$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(
    PDO::ATTR_PERSISTENT => true
));

Rrefer once this :

http://php.net/manual/en/pdo.connections.php

srinivas
  • 109
  • 12