简体   繁体   English

使用PHP或MySQL转换utf8字符

[英]convert utf8 characters using PHP or MySQL

I'm not sure if someone has asked this question before or not but I didn't find any. 我不确定是否有人问过这个问题,但是我没有找到任何问题。 While I hadn't set connection charset in PDO and table collation was utf8_percian_ci , so all data has been stored in unreadable characters like سلام that is سلام in Persian. 虽然我没有在PDO中设置连接字符集,并且表排序规则是utf8_percian_ci ,所以所有数据都以不可读的字符(如سلام存储,该字符在波斯语中为سلام

Before setting charset by adding mysql:charset=utf8mb4; 在通过添加mysql:charset=utf8mb4;设置字符集之前mysql:charset=utf8mb4; to PDO DNS I was able to retrieve all data correctly but now I see سلام instead of سلام in browser. 到PDO DNS,我能够正确检索所有数据,但是现在我看到سلام而不是浏览器中的سلام

My website is a blog and now it seems I have to reenter all the texts and posts and then restore them to be saved correctly. 我的网站是一个博客,现在看来我必须重新输入所有文本和帖子,然后将其还原以正确保存。 That's a disaster! 真是灾难!

I used mb_detect_encoding() for both سلام & سلام and found out that both of them are UTF-8. 我对سلامسلام都使用了mb_detect_encoding() ,发现它们都是UTF-8。 This is so funny to search "How to convert utf8 to utf8 ?" 搜索“如何将utf8转换为utf8?”真是太有趣了。 and absolutely I get no expected result. 绝对没有得到预期的结果。

Is there anyway to convert سلام to سلام using MySQL ? 无论如何,可以使用MySQL将سلام转换为سلام吗? If not, I thought another way could be using PHP to read old data then convert and insert into database again. 如果没有,我想另一种方法可能是使用PHP读取旧数据,然后转换并再次插入数据库。

What should I do ? 我该怎么办 ?

You can simply do: 您可以简单地执行以下操作:

UPDATE my_table SET my_column = BINARY CONVERT(my_column USING latin1)

(where latin1 is the character set in which your connection was set at the time of insertion). (其中latin1是插入时设置连接的字符集)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM