undefined class constant 'mysql_attr_init_comm

2019-02-20 18:56发布

I have seen the other SO questions dealing with the bug in php 5.3 and have php_pdo_mysql enable in the php.ini file located in MAMP/conf/php5.4.4. When I look at the phpInfo, under PDO it only has the sqlite driver enabled. If anyone has any suggestions on how to troubleshoot this further it would be greatly appreciated.

This is the line in question:

$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');

I'm including some additional info as I think it applies to the solutions I've tried so far

try 
{ 
    $db = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 
} 
catch(PDOException $ex) 
{ 
    die("Failed to connect to the database: " . $ex->getMessage()); 
} 

I tried replacing all the variable parameters of the PDO object with string values ie. {$host} becomes 'localhost' etc. and got the error message in the catch block with $ex->getMessage() being 'could not find driver'

标签: php pdo mamp
2条回答
成全新的幸福
2楼-- · 2019-02-20 19:38

i had to do is : Undefined class constant 'MYSQL_ATTR_INIT_COMMAND' with pdo

sudo apt-get install php5-mysql

sudo /etc/init.d/apache2 restart

=> issues have been resolved

查看更多
Bombasti
3楼-- · 2019-02-20 19:41

Check out this DSN:

$dsn = 'mysql:dbname=testdb;host=127.0.0.1;charset=utf8';

Instead of MySQL attr init command.

查看更多
登录 后发表回答