MySQL UTF8 数据无法正确显示
我希望MySQL中的所有数据都是UTF8编码的。我已将数据库、表和列的所有字符集和排序规则设置为 UTF8。在将任何内容写入数据库之前,我在 PHP 中使用 mb_detect_encoding 来检查它是否为 UTF8。因此,我相信所有数据都是UTF8编码的。
然而,问题是:以 Ríkarðsdóttir 这个词为例,当从数据库查询并通过 PHP 在 UTF8 编码的网页上显示时,它会正确显示。如果我通过 phpMyAdmin 查询同一条记录,我会得到 Rákarásdóttir。如果我使用 MySQL 命令行也是如此。
Running SHOW VARIABLES returns to me:
character_set_client utf8,
character_set_connection utf8,
character_set_database utf8,
character_set_filesystem binary,
character_set_results utf8,
character_set_server latin1,
character_set_system utf8
只有服务器是 latin1,并且我位于共享托管站点上,并且不相信我可以更改它。这可能是问题所在吗?
我不明白的是:为什么我的 UTF8 网页正确显示 Ríkarðsdóttir,但 UTF8 编码的 phpMyAdmin 网页将其显示为 Ríkarðsdóttir?数据是否不是真正的 UTF8 编码,或者数据库不相信它是真正的 UTF8 编码?需要采取什么措施来纠正这个问题?
I want all the data in MySQL to be UTF8 encoded. I've set all the character sets and collations to be UTF8 for the database, tables and columns. Before anything is written to the database, I use mb_detect_encoding in PHP to check if it is UTF8. Thus, I believe all the data is UTF8 encoded.
However, here is the problem: take this word Ríkarðsdóttir, it shows up correctly when queried from the database and displayed through PHP on a UTF8 encoded webpage. If I query this same record through phpMyAdmin, I get RÃkarðsdóttir. The same is true if I use the MySQL command line.
Running SHOW VARIABLES returns to me:
character_set_client utf8,
character_set_connection utf8,
character_set_database utf8,
character_set_filesystem binary,
character_set_results utf8,
character_set_server latin1,
character_set_system utf8
Only the server is latin1, and I am on a shared hosting site and don't believe I can change that. Could that be the problem?
Here is what I do not understand: why does my UTF8 webpage correctly display Ríkarðsdóttir, but a UTF8 encoded phpMyAdmin webpage display it as RÃkarðsdóttir? Is the data not truly UTF8 encoded or does the database not believe it is? What needs to be done to correct this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在连接后立即运行此查询:
您的数据库需要将数据存储为 UTF8,并且您的网页标头也应该有 UTF8 声明,但您与数据库的连接也需要使用 UTF8。您可以在命令行和/或通过 PHPMyAdmin 运行它。该“查询”之后的所有通信都将采用 UTF8 编码。
Try running this query right after you connect:
Your database needs to store the data as UTF8, and your web page header should also have a UTF8 declaration, but your connection to the database also needs to use UTF8. You can run that on the command line and/or through PHPMyAdmin. All communication after that "query" will then be UTF8 encoded.