Reputation: 118
I am using PHP and MySQL for saving Arabic data.
My database collation is : utf8_general_ci
My database Character set is : utf8
At first I did not use SET NAMES utf8
before insertion so the data was inserted in strange characters in the database but was displayed properly in my application. After using SET NAMES utf8
, data is inserted properly but the old data is not displayed in Arabic characters even in my application (The new data is ok) .
How can I update all the data to be displayed in Arabic letters in both my application and MySQL database?
Update
When I check the encoding of the both of strings ( the one that is inserted and the one that I want to convert to ) using mb_detect_encoding
function I get that both of the strings are UTF-8 .
Here is an example of the two strings :
the text inserted : الإسم بالعربية
the text I need to convert to : الإسم بالعربية
Upvotes: 2
Views: 2575
Reputation: 142278
You suffer from "double encoding".
Here's what happened.
SET NAMES latin1
lied by claiming that the client had latin1 encoding; andCHARACTER SET utf8
.Let's walk through what happens to e-acute: é
.
C3A9
.SET NAMES latin1
saw it as 2 latin1-encoded characters Ã
and ©
(hex: C3
and A9
)CHARACTER SET utf8
, those 2 characters needed to be converted.
Ã
was converted to utf8 (hex C383
) and ©
(hex C2A9
)C383C2A9
)When reading it back out, the reverse steps were performed, and the end user possibly noticed nothing wrong. What is wrong:
ORDER BY
may not work as expected.Something like this will repair your data:
UPDATE ... SET col = CONVERT(BINARY(CONVERT(
CONVERT(UNHEX(col) USING utf8)
USING latin1)) USING utf8);
More discussion and More examples of fixing it
Upvotes: 3
Reputation:
Maybe this is helpful [http://forums.mysql.com/read.php?103,209072,209072
*Same error and solution on MySQL forum. (since 2008)
Upvotes: 0