MySQL collation and PHP charset conflict

我与影子孤独终老i 提交于 2019-12-13 21:17:25

问题


I have a bunch of Danish text taken from a latin-1 MySQL database and it displays correctly when echoed in PHP. The problem starts when I need to echo some other Danish characters, which are not taken from the database.

What I do is actually output the header

Content-Type: text/html; charset=iso-8859-1

to also let the non-queried characters to display correctly as well.

Problems is, when I do that the queried characters display incorrectly.


回答1:


Just because the data is stored in a latin-1 collated table doesn't mean that it's latin-1 encoded. This is due to MySQL not doing any character translation when the connection SET NAMES setting is the same as the collation.

I suspect that you have some UTF8 characters stored in a latin1 database which is confusing the issue.

For more help please can you add details of the:

  • MySQL connection encoding that you have set
  • Details of where the "non-queried" characters are coming from



回答2:


Use unicode. UTF-8 => the right way.

So, set utf8_unicode_ci in database, UTF-8 as page charset and before your query set mysql_query("SET NAMES UTF8");



来源:https://stackoverflow.com/questions/5935117/mysql-collation-and-php-charset-conflict

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