MySQL UTF8 数据无法正确显示

发布于 2024-11-30 14:22:14 字数 770 浏览 1 评论 0原文

我希望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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙与沫 2024-12-07 14:22:14

尝试在连接后立即运行此查询:

SET NAMES UTF8

您的数据库需要将数据存储为 UTF8,并且您的网页标头也应该有 UTF8 声明,但您与数据库的连接也需要使用 UTF8。您可以在命令行和/或通过 PHPMyAdmin 运行它。该“查询”之后的所有通信都将采用 UTF8 编码。

Try running this query right after you connect:

SET NAMES UTF8

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文