Converting “wrong” MySQL encoding from old server to correct UTF-8 on new server

回眸只為那壹抹淺笑 提交于 2020-01-04 01:53:07

问题


I set up a web project on a web server with apparently wrong encoding. But somehow (I really don't know why), we figured how we had to deal with it and the encoding worked for us. But now we moved the data to a new server with a correctly set up UTF8 database and surprise, the encoding is wrong. How can we "correct" the data, is there any best practice?

Example old server: http://www.orat.io/stmt/200 new server: http://www.firefile.net/stmt/200

Thanks a lot!


回答1:


This actually happens in practice - I've seen it sometimes :-) What usually happens:

  • database is in utf-8
  • input/output is in encoding1 - say it is iso-8859-2 (i.e. the meta-charset in HTML)
  • but, because the previous admin didn't know how to properly set the database connection encoding (this is the charset which is specified using set names SQL command), the database connection is configured as if the input/output is in different encoding2 - say iso-8859-1 - default on many linux systems.

To get the correct data out of the database, you must access it in the same erroneous way as you did before - e.g. for the example above, set iso-8859-1 encoding but in fact get the iso-8859-2. Everything "works" until someone starts to access the database correctly.

You have not provided enough detail about what is encoding1/encoding2 in your case and you probably don't know it. So, either look at the old setup or try to figure out by trial and error.

The easiest way to re-code your database is probably to do mysqldump in encoding2, then claim it's encoding1 (just put the set names encoding1 at the beginning of sql file) and import the database into an empty one by source-ing the sql file. All database fields of course stay in utf-8 all the time.

Be careful and use PHPMyAdmin as an independent tool to see the "real" state of the database. :-) Good luck with this rebus.



来源:https://stackoverflow.com/questions/20824425/converting-wrong-mysql-encoding-from-old-server-to-correct-utf-8-on-new-server

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