PHP MySQL database strange characters

匆匆过客 提交于 2020-01-24 04:22:07

问题


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?


回答1:


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.




回答2:


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.




回答3:


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...




回答4:


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!)



来源:https://stackoverflow.com/questions/1910549/php-mysql-database-strange-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!