MySQL convert charset issue

↘锁芯ラ 提交于 2020-01-04 10:58:11

问题


I have an application which runs on PHP 5 and accesses and stores a MySQL database using the mysqli extension. The database contains numerous tables with the encoding UTF-8 (collation utf8_swedish_ci).

Unfortunately, it seems that the mysqli connection was configured to encode everything using ISO-8859-1,which means that I've got UTF-8 tables containing latin1 data. I am trying to repair this now, by shifting over everything to UTF-8 (as it should be!)

Is there a built-in way of handling this? If there isn't, how would you recommend I approach this issue?


Edit: A sample of what the data looks like while browsing through it all using PHPMyAdmin:

handelë (should be handelë)

√skal (should be √skal)

Also, the data is output correctly in the HTML document, as long as I use the output encoding UTF-8, but maintain the mysqli connection charset as latin1. It's all rather confusing,.

Very grateful for your help!


回答1:


All right! So this is what must have happened:

user interface (UTF-8) → controller (UTF-8) → model (ISO-8859-1) → Database (UTF-8, but it receives ISO-8859-1)

So the fields were configured to use the UTF-8 encoding, but they receive ISO-8859-1 encoded data. I wanted to convert the incorrectly encoded data to UTF-8.

Since the data was in fact ISO-8559-1 encoded, I resolved my problem with the following little MySQL "hack":

UPDATE `table` SET `column` = convert(cast(convert(`column` using  latin1) as binary) using utf8)

Courtesy ABS on StackOverflow.

Thank you for your time looking into my problem, guys! :)



来源:https://stackoverflow.com/questions/18042547/mysql-convert-charset-issue

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