PDO and UTF-8 special characters in PHP / MySQL?
You're missing an S: it's SET NAMES
and not SET NAME
:
$this->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8'");
You also need to un-comment it of course. Also, PDO::MYSQL_ATTR_INIT_COMMAND
can not be set with PDO::setAttribute()
after you've established your database connection (the constant name says it all), you've to specify it in the constructor using the $driver_options
argument, like this:
$this->db = new PDO($this->dsn, $this->username, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
An alternative to this is to just execute that very same query immediately after connecting:
$this->db = new PDO($this->dsn, $this->username, $this->password);
$this->db->exec("SET NAMES 'utf8';");
I had the same trouble and found out after trying out 1000000 different options that the default mysql engine still remains MyISAM.
Verifying that InnoDB is the Default Storage Engine as such:
Issue the command SHOW VARIABLES LIKE 'have_innodb';
to confirm that InnoDB is available at all.
Then issue the command SHOW ENGINES;
to see all the different MySQL storage engines.
Look for DEFAULT
in the InnoDB
line and NOT in the MyISAM
line. I realized that my provided had a setting preventing me from changing the default engine using SET storage_engine=MYISAM;
. In my case, I contacted my provider and was directed to where I could change the option to be able to change the default engine. Hope this helps!