MySQL转换字符集问题 [英] MySQL convert charset issue
问题描述
我有一个运行PHP 5的应用程序,使用 mysqli 扩展访问和存储MySQL数据库。数据库包含许多带有编码 UTF-8 (排序规则 utf8_swedish_ci )的表格。不幸的是,似乎mysqli连接被配置为使用ISO-8859-1对所有内容进行编码,这意味着我有 UTF-8
strong>包含 latin1 数据的表格。我现在尝试修复这一切,通过将一切转移到UTF-8(应该是!)有一个内置的方法来处理这个?如果没有,您会如何建议我处理这个问题?
使用PHPMyAdmin浏览所有数据时,数据的样本示例:
handeln«
code>handelë)
âšskal
c>√skal)
此外,数据在HTML文档中正确输出,只要我使用输出编码UTF-8 ,但保持mysqli连接字符集为latin1。
非常感谢您的帮助!
p>好吧!所以这是必须发生的:
用户界面(UTF-8)→控制器(UTF-8)→模型(ISO-8859-1)→数据库-8,但它接收ISO-8859-1)
因此,字段配置为使用UTF-8编码,但它们接收ISO-8859-1编码数据。我想将错误编码的数据转换为UTF-8。
由于数据实际上是ISO-8559-1编码,我解决了我的问题, hack:
UPDATE`table` SET`column` = convert(cast(convert(`column` using latin1)as二进制)使用utf8)
礼貌 ABS 在StackOverflow上。
感谢您抽出宝贵时间研究我的问题,伙计们! :)
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!
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! :)
这篇关于MySQL转换字符集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!