3

I made an application that sends few text inputs from android to my online database. My application ran good, but some users reported that they can't use Arabic text. So I tried, and When I send Arabic text in the database it appears as ????? ????? ??????

My php script:

<?php
$connect = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);

if(mysql_errno($connect))
{
die("Failed to connect to MySQL: " . mysql_error());
}
else
{
    echo "success";
mysql_query("SET NAMES 'utf8'"); mysql_query('SET CHARACTER SET utf8');
}

$name = isset($_POST['name']) ? $_POST['name'] : '';
$sex = isset($_POST['sex']) ? $_POST['sex'] : '';
$nationality = isset($_POST['nationality']) ? $_POST['nationality'] : '';

$query = mysql_query( "insert into people (name, sex, nationality) values (N'$name' ,N'$sex', N'$nationality') ", $connect);

print_r($_POST);
if(!$query) echo mysql_error();
mysql_close($connect);
?>

my Android code:

                DefaultHttpClient httpclient= new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.xxxxxxxx.com/thescript.php");
                Log.e("done 1st","its here");

                try
                {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("name", Name.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("sex",  Sex.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("nationality",  Nationality.getText().toString()));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                    HttpResponse response = httpclient.execute(httppost);
                    Log.e("done", String.valueOf(response));
                }

I tried few stuff as you can see but none worked.

and when using insert into using online pc .php I get like this : لببي

---------

My Database settings are:

The table collation is utf8_general_ci and its type is MyISAM

and the collumns (name,sex,nationality) inside it:

type is varchar and collation is utf8_general_ci

ex. the output should be علي but it's ???

John Jared
  • 790
  • 3
  • 16
  • 40

6 Answers6

2

Do this way

DefaultHttpClient httpclient= new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://www.xxxxxxxx.com/thescript.php");
                Log.e("done 1st","its here");

                try
                {
                    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                    nameValuePairs.add(new BasicNameValuePair("name", Name.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("sex",  Sex.getText().toString()));
                    nameValuePairs.add(new BasicNameValuePair("nationality",  Nationality.getText().toString()));

                    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); //UPDATE HERE
                    HttpResponse response = httpclient.execute(httppost);
                    Log.e("done", String.valueOf(response));
                }
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
1

Type shouldn't be varchar, since it does not accept Unicode characters. Update it to nvarchar!

Like Andries said, collation should be set to utf8_general_ci or other Unicode collation as well.

EDIT:

So, since you are on MySQL v5.x you should have a config file (like my.cnf). You should ensure you are not using your super user to access the database. Add this to you config file.

[mysqld]
init_connect='SET collation_connection = utf8_general_ci; SET NAMES utf8;'
default-character-set=utf8
character-set-server=utf8
collation-server=utf8_general_ci

This will trigger any connection to use utf8_general_ci for any connection.

Restart MySQL and if mysqld.log does not return any error you should rally!

Vereos
  • 503
  • 4
  • 15
1

I believe your java code is right.

On the backend, Try to use this code: (and if you can switch to PDO)

$conn= mysql_connect($db_host,$db_uid,$db_pass, true) or die('could not connect');
mysql_select_db($db_name);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
mysql_set_charset('utf8', $conn); 

Debug to know the encoding using:

<?php
$charset = mysql_client_encoding($conn);
echo "The current character set is: $charset\n";
?>

Then

Execute the following commands as root:

ALTER DATABASE YOUR_DATABASE_ NAME CHARACTER SET utf8 COLLATE utf8_general_ci;
update mysql.proc set character_set_client = "utf8", collation_connection = "utf8_general_ci", db_collation = "utf8";

Then execute the code in the following SO answer

Community
  • 1
  • 1
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

You should change your table collation to utf8_general_ci or any other unicode collation. Latin1 cannot represent Arabic script.

0

I had this problem for a week long time and searched and searched, the solution that @biraj Suggested in THE ONLY way to solve this problem! No need to change database or set UTF-8 in php or .... Using this

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

and this problem is gone for ever.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Saghachi
  • 851
  • 11
  • 19
-1

You need to pass the correct (UTF-8) Charset to your UrlEncodedFormEntity constructor.

Jan Schejbal
  • 4,000
  • 19
  • 40
  • can you how me how to do that ? – John Jared Sep 28 '13 at 08:31
  • @JohnJared huh? Something like `httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Charset.forName("UTF-8")));` - you will probably need to get rid of the potential Exception by wrapping it in a try..catch block. – Jan Schejbal Sep 28 '13 at 08:50
  • I get **The method setEntity(HttpEntity) in the type HttpEntityEnclosingRequestBase is not applicable for the arguments (UrlEncodedFormEntity, Charset)** from .setentity – John Jared Sep 28 '13 at 09:01