Link to home
Start Free TrialLog in
Avatar of bprof2007
bprof2007Flag for United States of America

asked on

Bad encoding in storing foreign language in MySql

Hi,

I'm trying to store data in a foreign language (RTL languages) in MySql, the first time I stored the data I found that it was saved like this:

??? ?????? ????? ?????? ?????? ???? ??????? ???? ?? ??????? ?? ??? ????? ????? ??? ??? ?? ??? ???? ????? ??? ??? ???? ????? ???? ????.

So I did some search, and found that a solution suggested by a guy who faced the same problem. The solution was to add this line:

mysql_query("SET NAMES 'UTF8'");

I added it but with no luck. Any suggestions, ideas, a better way to make work is highly appreciated.

Avatar of bprof2007
bprof2007
Flag of United States of America image

ASKER

Just an addition when I changed the collation to be utf8_unicode_ci, it did not save anything.
Avatar of Alex
First of all when you create a table you have to set the right Collation of the database and after this to set into you connection that you said (mysql_query("SET NAMES 'UTF8'") and maybe one more thing ...

mysql_query("SET CHARACTER SET 'UTF8';")

So if you had already create your table the records that you have, will be as is ...(?????) :( you must create from the begin the table ... and set the right collation.
SOLUTION
Avatar of Muhammad Wasif
Muhammad Wasif
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks alex_code for your input.

I've dropped the table, and then created a new one with collation set to utf8_unicode_ci, but still it does not show anything even ???????. But if I inserted text in English then it shows.

Thanks wasifg for your help.

I've tried using utf8_bin, but same thing.

Any ideas??
Which language you want to save?
Persian and Arabic.
If I saved it directly using phpmysql it will save it in its correct format, but if it was inserted using a web form then it does not. Which makes me wonder if it has anything to do with the html encoding??
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So, the above code is for the conn.php file

and this is to add records into the table using the conn file that has also the charset...

<?php
require ("conn.php");

      $query = "INSERT INTO your_table (title,name) VALUES ('".$_POST["title"]."','".$_POST["name"]."')";
      mysql_query($query) or die('Error, insert query failed');

?>
Thanks again so much alex_code for your help.

Now here what happened, I had already a conn.php file so I've added the two lines that you kindly provided:

mysql_query("SET NAMES 'utf8';", $Link);
mysql_query("SET CHARACTER SET 'utf8';", $Link);

To the end of that file.

And removed this line:

mysql_query("SET NAMES 'utf8'");

which precedes the insert query.

It did save data this time, but it was all like this:

???????????

So what I did is that I removed the two lines from conn.php
mysql_query("SET NAMES 'utf8';", $Link);
mysql_query("SET CHARACTER SET 'utf8';", $Link);

And also the line:
mysql_query("SET NAMES 'utf8'");

It saved the data like this:

ãäÊÎÈ ãÕÑ

When I tried to see what was that I ran a query to show everything in DB on a web page, and  surprisingly it shown the desired results.

So I'm wondering why and how it did it this way? And why it does not appear in the DB in the correct format?