简体   繁体   中英

Sending Gujarati text to MySQL from Android using PHP

I am developing a sample app which has two textboxes that takes input from users. Now I have created a Gujarati keyboard using buttons so that user enters data in Gujarati language. When user presses Save button the data should be stored in MySQL. This is my complete code:

public void saveMy(View v)
{
    name=e1.getText().toString();
    no=e2.getText().toString();
    try {
            httpclient = new DefaultHttpClient();
            httppost = new HttpPost("http://10.0.2.2/GujaratiApp/myPHP.php");


            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("Name",name));
            postParameters.add(new BasicNameValuePair("No",no));
            httppost.setEntity(new UrlEncodedFormEntity(postParameters));

            HttpResponse response = httpclient.execute(httppost);
            Log.i("postData", response.getStatusLine().toString());
        }
    catch(Exception ex)
    {
        Log.e("log_tag", "Error:  "+ex.toString());
    }   


}

And my PHP file is:

<?php 

  mysql_connect("localhost","root","") or die(mysql_error());

  mysql_select_db("newDB");

  $name =   $_POST['Name'];

  $no = $_POST['No'] ;

  print("Name is:   ".$name."             And No is:".$no);

  $query_add="INSERT INTO  myData (`name` ,`no` ) VALUES ('$name','$no')";

  $query_exec=mysql_query($query_add) or die(mysql_error()); 

  mysql_close();

?>

Query used to create database is:

CREATE TABLE myData (name VARCHAR(20) NOT NULL,no VARCHAR(5) NOT NULL) ENGINE=INNODB CHARACTER SET= utf-8;

My data gets enter when I press Save button but in MySql i get ??? instead of Gujarati text.

when I enter English data then it works properly..

Just a little change in your code. Try this.

httppost.setEntity(new UrlEncodedFormEntity(postParameters,HTTP.UTF_8));

Hope it helps

You need to set charchter encoding on your PHP script:

ini_set('default_charset', 'utf-8');

so put the above line of code in your php script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM