How to store characters like ♥☆ to DB?

烂漫一生 提交于 2019-12-11 18:35:23

问题


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?


回答1:


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] => ♥☆
)



回答2:


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


来源:https://stackoverflow.com/questions/3016425/how-to-store-characters-like-to-db

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