Convert latin1 characters on a UTF8 table into UTF8

六月ゝ 毕业季﹏ 提交于 2019-11-26 04:38:25

问题


Only today I realized that I was missing this in my PHP scripts:

mysql_set_charset(\'utf8\');

All my tables are InnoDB, collation \"utf8_unicode_ci\", and all my VARCHAR columns are \"utf8_unicode_ci\" as well. I have mb_internal_encoding(\'UTF-8\'); on my PHP scripts, and all my PHP files are encoded as UTF-8.

So, until now, every time I \"INSERT\" something with diacritics, example:

mysql_query(\'INSERT INTO `table` SET `name`=\"Jáuò Iñe\"\');

The \'name\' contents would be, in this case: Jáuò Iñe.

Since I fixed the charset between PHP and MySQL, new INSERTs are now storing correctly. However, I want to fix all the older rows that are \"messed\" at the moment. I tried many things already, but it always breaks the strings on the first \"illegal\" character. Here is my current code:

$m = mysql_real_escape_string(\'¿<?php echo \"¬<b>\\\'PHP &aacute; (á)ţăriîş </b>\"; ?> ă-ţi abcdd;//;ñç´พดแทฝใจคçăâξβψδπλξξςαยนñ ;\');
mysql_set_charset(\'utf8\');
mysql_query(\'INSERT INTO `table` SET `name`=\"\'.$m.\'\"\');
mysql_set_charset(\'latin1\');
mysql_query(\'INSERT INTO `table` SET `name`=\"\'.$m.\'\"\');
mysql_set_charset(\'utf8\');

$result = mysql_iquery(\'SELECT * FROM `table`\');
while ($row = mysql_fetch_assoc($result)) {
    $message = $row[\'name\'];
    $message = mb_convert_encoding($message, \'ISO-8859-15\', \'UTF-8\');
    //$message = iconv(\"UTF-8\", \"ISO-8859-1//IGNORE\", $message);
    mysql_iquery(\'UPDATE `table` SET `name`=\"\'.mysql_real_escape_string($message).\'\" WHERE `a1`=\"\'.$row[\'a1\'].\'\"\');
}

It \"UPDATE\"s with the expected characters, except that the string gets truncated after the character \"ă\". I mean, that character and following chars are not included on the string.

Also, testing with the \"iconv()\" (that is commented on the code) does the same, even with //IGNORE and //TRANSLIT

I also tested several charsets, between ISO-8859-1 and ISO-8859-15.


回答1:


From what you describe, it seems you have UTF-8 data that was originally stored as Latin-1 and then not converted correctly to UTF-8. The data is recoverable; you'll need a MySQL function like

convert(cast(convert(name using  latin1) as binary) using utf8)

It's possible that you may need to omit the inner conversion, depending on how the data was altered during the encoding conversion.




回答2:


After i searched about an hour or two for this answer. I needed to migrate a old tt_news db from typo into a new typo3 version. I already tried convert the charset in the export file and import it back, but didn't get it working.

Then i tried the answer above from ABS and startet a update on the table:

UPDATE tt_news SET 
    title=convert(cast(convert(title using  latin1) as binary) using utf8), 
    short=convert(cast(convert(short using  latin1) as binary) using utf8), 
    bodytext=convert(cast(convert(bodytext using  latin1) as binary) using utf8)
WHERE 1

You can also convert imagecaption, imagealttext, imagetitletext and keywords if needed. Hope this will help somebody migrating tt_news to new typo3 version.




回答3:


the way is better way use connection tow you database normal

then use this code to make what you need you must make your page encoding utf-8 by meta in header cod html (dont forget this)

then use this code

    $result = mysql_query('SELECT * FROM shops');
    while ($row = mysql_fetch_assoc($ 
    $name= iconv("windows-1256", "UTF-8", $row['name']);

   mysql_query("SET NAMES 'utf8'"); 
   mysql_query("update   `shops` SET `name`='".$name."'  where ID='$row[ID]'  ");
    }



回答4:


I highly recommend using 'utf8mb4' instead of 'utf8', since utf8 cannot store some chinese characters and emojis.



来源:https://stackoverflow.com/questions/9407834/convert-latin1-characters-on-a-utf8-table-into-utf8

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