如何存储像♥☆这样的字符到数据库?

发布于 2024-09-05 03:11:04 字数 249 浏览 12 评论 0原文

上一期 - 无法存储非英语字符:

如何存储非英语字符?

这是通过使用 UTF8 修复的。但今天意识到像 ♥☆ 这样的符号没有正确存储。它们会被转换为像 ♥↑ 这样的字符。

如何解决这个问题?

Previous issue - was not able to store non-english characters:

How to store non-english characters?

That was fixed by using UTF8. But realized today that symbols like ♥☆ are not stored correctly. They get converted to characters like ♥☆.

How can this be fixed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

沙与沫 2024-09-12 03:11:04

在我看来,它们的存储是正确的,但是当您读出它们时,您没有正确地解释它们。 最终将成为 UTF-8 编码中的多字节字符。我敢打赌,如果您查找该多字节编码,您会发现它分别与 ♥ 的单字节编码相同。

编辑:添加细节。

如下表所示,将 UTF-8 字符解释为如同 Windows Latin-1 编码一样,可以得到您所看到的结果。

UTF-8 character      Hex
♥                    e2 99 a5
☆                    e2 98 86

Windows Latin-1      Hex
â                    e2
™                    99
¥                    a5
˜                    98
†                    86

It looks to me like they're being stored correctly, but that you're not interpreting them correctly when you read them out. and are going to end up as multibyte characters in UTF-8 encoding. I'll bet if you look up that multibyte encoding, you'll see it's the same as the single-byte encoding for ♥ and ☆ respectively.

Edit: adding details.

As you can see in the following table, interpreting the UTF-8 characters as if they were encoded as Windows Latin-1 gives the results you're seeing.

UTF-8 character      Hex
♥                    e2 99 a5
☆                    e2 98 86

Windows Latin-1      Hex
â                    e2
™                    99
¥                    a5
˜                    98
†                    86
赢得她心 2024-09-12 03:11:04

UTF8 是否在整个范围内一致使用(MySQL、PHP、Apache、

、标头......)?

对我来说,这是开箱即用的:

$query = "update tbl set col = '♥☆' where id = 1";
mysql_query($query) or die(mysql_error());
$query = "select col from tbl where id = 1";
$res = mysql_query($query) or die(mysql_error());
print_r(mysql_fetch_row($res));

调试输出:

Content-type: text/html; charset=utf-8
Array
(
    [0] => ♥☆
)

Is UTF8 used consistently across the whole spectrum (MySQL, PHP, Apache, <meta>s, headers..)?

For me this worked out of the box:

$query = "update tbl set col = '♥☆' where id = 1";
mysql_query($query) or die(mysql_error());
$query = "select col from tbl where id = 1";
$res = mysql_query($query) or die(mysql_error());
print_r(mysql_fetch_row($res));

Debug output:

Content-type: text/html; charset=utf-8
Array
(
    [0] => ♥☆
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文