Reputation: 800
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 ???
Upvotes: 3
Views: 2139
Reputation: 945
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.
Upvotes: 0
Reputation: 28484
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));
}
Upvotes: 2
Reputation: 45942
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
Upvotes: 1
Reputation: 4033
You need to pass the correct (UTF-8) Charset to your UrlEncodedFormEntity constructor.
Upvotes: -1
Reputation: 513
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!
Upvotes: 1
Reputation: 153
You should change your table collation to utf8_general_ci or any other unicode collation. Latin1 cannot represent Arabic script.
Upvotes: 0