-3

I have my page encoding set to utf8 and even in the meta tag as utf8.

However, when i'm taking a value from a database it's putting a diamond with a question mark instead - im assuming doesnt know the character.

The character is a é. If i do a echo é; it displays as normal on the page. Also if i write it manually in html. However, when i grab the same value from a database call using PDO i get a �

I'm assuming its a PDO setting. I've tried:

$db->exec("SET NAMES 'utf8';");

but this doesnt resolve it.

Any suggestions?

samayo
  • 16,163
  • 12
  • 91
  • 106
user2183216
  • 359
  • 3
  • 9
  • 22
  • 2
    what's the charset on the table itself? the entire rendering pipeline has to be the same charset, or at least chained with appropriate conversion logic. e.g. browser->server->php->database->table->database->php->server->browser – Marc B Jul 12 '13 at 21:36
  • "even meta tag" means for the browser little less than nothing – Your Common Sense Jul 12 '13 at 21:39
  • If you are on php 5.3.6+, you should set your character set in the dsn string (the first argument of the PDO constructor) like `;charset=utf8`. – jeroen Jul 12 '13 at 21:45
  • managed to resolve this by adding utf8_encode($variablename) to the variables – user2183216 Jul 12 '13 at 21:45
  • Please do `var_dump(unpack('H*', "é"));` and tell us the output. – Gumbo Jul 13 '13 at 08:54

1 Answers1

2

Many things can go wrong on the way. Usually you need to have your source file encoded using utf-8, and opening the database connection using utf-8 and defining the database tables as utf-8.

A great article from @deceze that helped me clarify things is http://kunststube.net/frontback/.

The most obvious things you can try in your case are:

  1. Save you source file with utf8 encoding. This option exists in editors like Notepad++ or Crimpson Editor.
  2. create the PDO connection with utf8 option:

    $connection = new PDO('mysql:host='.$this->host.';dbname='.$this->db_name.';charset=utf8', $this->user, $this->pass,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));

  3. make sure your table is utf-8 encoded and your form has the option :

    < form action="action.php" accept-charset="utf-8">

Update: maybe utf8_encode fixed your problem, but there is a wrong conversion somewhere from the PHP to the database and back. or an wrong file encoding. You should fix the root of the problem, and utf8_encode will not be needed anymore.

Daan
  • 12,099
  • 6
  • 34
  • 51
Aris
  • 4,643
  • 1
  • 41
  • 38