Php mysql database strange characters

I am trying to output product information stored in a MySQL database, but it writes out some weird characters, such as a diamond with a question mark inside it.

I think this may be a problem with the / UTF 8 encoding, but I specified the encoding I want:

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

Is it correct? What should I check?

+6
php mysql utf
source share
4 answers

If only the data coming from the database contains strange characters in it, make sure the MySQL connection is also in UTF8 using:

 mysql_query("SET NAMES UTF8"); 

before any other requests. Otherwise, if characters also appear in handwritten files, make sure the files are saved as UTF-8 in your editor. You can also try customizing the charset header via PHP:

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

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

+20
source share

The last time I had this problem, the solution was similar to what Tatu Ullmanen said, but a little different ...

So, if his solution doesn’t work, try replacing

 mysql_query("SET NAMES UTF8"); 

from

 mysql_query("SET NAMES latin1"); 

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

hope this helps ...

+4
source share

I assume you want the result to be in utf8

  • save php script utf8 encoded
  • make sure your http header (or some meta tags) says the output is utf8
  • all tables in MySql must be utf8
  • last but not least, the connection between the client and server must be utf8. (This could be handled somewhere in php.ini setup or by querying the following db request: 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 points, you should never have problems with broken encodings.

+4
source share
+1
source share

All Articles