PHP / MySQL特殊字符显示不正确[重复]

Possible Duplicate:
Special characters in PHP / MySQL

I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.

One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is. Oh and I already have included

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

and this didn't work.

Hopefully you've all the information to help me, if not just say :) Thanks in advance!

Milaan

You need to have everything in utf-8:

  • The database field
  • The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
  • The content type (like you have already)

I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years. But this is not neccessary when You have everything in the same encoding

  • source files encoding
  • table columns collations
  • web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)

I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering

i.e. if someone inputs html tags they'll be active in your page and so on.