Convert database data to UTF-8 |
[eluser]Swedie[/eluser]
Hello! Some of you might've seen my questions on the topic of encoding. Now I need a good way of turning the data in the database into UTF-8. All data is currently in ISO-8859-1 forma so upon displaying it the swedish characters are all symbols. Not so good. A quick fix is to run utf8_encode on the data before display it. But that's not gonna work in the long run. Instead I want to convert the data to the correct encoding in the database. Would you suggest to loop through all tables and do a utf8_encode on everything? I know one negative about this, and it's that if the loop fails and you run it again, the already encoded characters will be encoded and displayed incorrectly again. Code suggestion is welcome. Thanks ![]()
[eluser]dudeami0[/eluser]
First off backing up your data is a must. Second off, MySQL has these conversions built in. Code: ALTER DATABASE database DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; Hope this helps ![]()
[eluser]Swedie[/eluser]
It's backed up. I'm also in the test environment. Your above suggestion does not work, done that both ot the whole database but also the tables themselves. The input into the database that was once made was with a page encoding of ISO-8859-1. Changing just the database or table encoding only alters future inserts (as far as I know), not current data.
[eluser]dudeami0[/eluser]
Hmm, could try exporting and importing it: Code: mysqldump --user=username --password=password --default-character-set=charset --compatible=mysql40 dbname > dump.sql Code: mysql --user=username --password=password --default-character-set=utf8 dbname < dump.sql Changing the commands to meet your needs.
[eluser]LuckyFella73[/eluser]
Hej Swedie, Phil Sturgeon has posted a very nice tutorial how to set up a clean utf-8 invironment or how to convert an existing projekt into utf-8 encoding (database). Here is the link to his blog: http://philsturgeon.co.uk/news/2009/08/U...odeIgniter Hope that helps
[eluser]InsiteFX[/eluser]
You can alter the table all you want it will not change the database collation! In PhpMyAdmin go to operations tab and chage the collation on the bottom dropdown. It defaults to latin1_swedish_ci InsiteFX
[eluser]Swedie[/eluser]
Well sadly none of above worked. So I wrote a PHP function to loop through all of the tables and convert the data when needed using PHP's mb_convert and mb_encode functions. I think it worked ![]() Here's the code in case anyone wants it. You can true or false to convert data or just the tables (and columns). I haven't added collate, as it self-adjust to the correct one (atleast for me). Code: function fixEncoding($convertData = TRUE, $convertTableAndColumns = TRUE) { |
Welcome Guest, Not a member yet? Register Sign In |