UTF-8:在数据库中正确显示,但不是在HTML,尽管utf-8字符集 [英] UTF-8: showing correctly in database, however not in HTML despite utf-8 charset
问题描述
我使用MySQL 5.1并从一个UTF-8解码的txt文件中加载约2.7 mil行到一个本身被声明为 utf8_unicode_ci
的表以及所有的char-字段声明为 utf8_unicode_ci
,使用 LOAD DATA INFILE
...
在数据库本身中,字符都似乎是正确的,一切看起来不错。但是,当我使用php打印它们时,字符显示为???,虽然我在HTML头中使用utf-8声明:
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = UTF-8>
...
在另一个表(使用utf-8)从提交的表单中,字符在数据库中出现奇怪,但是当我使用 SELECT ...
打印时,它们再次正确显示。
所以,我想知道:什么是错的? UTF-8
字符在数据库中正确显示或奇怪,但是当你 SELECT
他们还好吗?或者在哪里是问题(当将文件加载到数据库,在HTML或中间的某个地方)?
非常感谢任何提示或建议! :)
注意: MySQL的 utf8
字符集是有限的,它只支持在BMP中占用不超过三字节。您应该改用 utf8mb4
。
- 在连接MySQL之后,在运行任何MySQL之前,
SET NAMES utf8
SET NAMES utf8mb4
查询。 - 确保您的网页实际上是以utf-8呈现的(如果有一个HTTP标头
Content-Type:text / html; charset = iso-8859-1
,浏览器不同意哪个应该胜出。 - 阅读这篇文章:在网络应用程序中处理Unicode前端(但请记住用MySQL中涉及的
utf8mb4
替换utf8
)。
如果phpMyAdmin将您输入的数据显示为正确的Unicode文本,那么我的赌注是您不在 SET NAMES utf8
I use MySQL 5.1 and loaded from a UTF-8 decoded txt-file about 2.7 mil lines into a table which itself is declared as utf8_unicode_ci
and as well all char-fields are declared as utf8_unicode_ci
, using LOAD DATA INFILE
...
In the database itself the characters all seem to be correct, everything looks nice. However, when I print them using php, the characters show up as ???, although I use utf-8 declaration in the HTML head:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
...
In another table (using utf-8), where I inserted text from a submitted form, the characters appear strangely in the database, but are shown correctly again, when I print them using SELECT...
.
So, I was wondering: what is wrong? Are UTF-8
chars shown correctly in the database or strangely but when you SELECT
them again they are OK? Or where is the problem (when loading the file into the db, in the HTML or somewhere in between)??
Thank you very much for any hint or suggestion! :)
- Make sure you send the
SET NAMES utf8
SET NAMES utf8mb4
command to MySQL after connecting, before running any MySQL queries. - Make sure your page is actually rendered as utf-8 (if there's an HTTP header
Content-Type: text/html;charset=iso-8859-1
, browsers disagree about which should win). - Read this article: Handling Unicode Front To Back In A Web App (but remember to replace
utf8
withutf8mb4
where MySQL is concerned).
If phpMyAdmin displays your entered data as correct Unicode text, then my bet is that you are not doing SET NAMES utf8
after connecting.
这篇关于UTF-8:在数据库中正确显示,但不是在HTML,尽管utf-8字符集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!