tags:

views:

402

answers:

4

Hello, I'm trying to output product information stored in a MySQL database, but it's writing out some strange characters, like a diamond with a question mark inside of it.

I think it may be an encoding/UTF8 issue, but I've specified the encoding I want:

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

Is this right? What should I check for?

+6  A: 

If only the data that's coming from database has strange characters in it, be sure that the MySQL connection is also in UTF8 by using:

mysql_query("SET NAMES UTF8");

before any other queries. Otherwise, if the characters appear also in 'handwritten' files, make sure that the files are saved as UTF-8 in your editor. You can also try setting the charset header through PHP:

header('Content-type: text/html; charset=UTF-8');

Also make sure that all fields in the tables you are querying are set as some UTF-8 variant, for example utf8_general_ci.

Tatu Ulmanen
Nice explanation, Tatu.
BenTheDesigner
A: 

Seconding what Tatu says.

This is good background reading on encoding: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

Pekka
A: 

The last time I had that trouble, the solution was similar to what Tatu Ulmanen said, but slightly different...

So if his solution does not work, try replacing

mysql_query("SET NAMES UTF8");

with

mysql_query("SET NAMES latin1");

I say this because the default characterset in MySql is latin1, and that is what is used most of the time....

hope that helps...

pǝlɐɥʞ
A: 

I assume you want the result to be in utf8

  • save you php script utf8 encoded
  • make sure your http header (or some meta tags) tells that output is utf8
  • all tables in MySql should to be utf8
  • last but not least, the connection between client and server should be utf8. (This could be handled somewhere in the php.ini setting or by making the following query against the db: SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'

If you follow all 4 point you should never ever have any problem with broken encodings.

Hippo