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