问题
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