How to Insert Arabic data into mysql database from android -
it has android application adds data database, when enter data in arab, show "?????" . tried ways.
file config.inc.php :
<?php $username = "*****"; $password = "****"; $host = "*****"; $dbname = "*****"; $options = array(pdo::mysql_attr_init_command => 'set names utf8'); try { $db = new pdo("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); mysql_set_charset('utf8'); } catch(pdoexception $ex) { die("failed connect database: " . $ex->getmessage()); } $db->setattribute(pdo::attr_errmode, pdo::errmode_exception); $db->setattribute(pdo::attr_default_fetch_mode, pdo::fetch_assoc); if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) { function undo_magic_quotes_gpc(&$array) { foreach($array &$value) { if(is_array($value)) { undo_magic_quotes_gpc($value); } else { $value = stripslashes($value); } } } undo_magic_quotes_gpc($_post); undo_magic_quotes_gpc($_get); undo_magic_quotes_gpc($_cookie); } ?>
addition file :
<?php require("config.inc.php"); $query = "insert news ( news_date, news_title, news_body ) values ( :news_date, :news_title, :news_body ) "; $query_params = array( ':news_date' => $_post['news_date'], ':news_title' => $_post['news_title'], ':news_body' => $_post['news_body'] ); try { $stmt = $db->prepare($query); $result = $stmt->execute($query_params); } catch (pdoexception $ex) { $response["success"] = 0; $response["message"] = "database error. couldn't add news!"; die(json_encode($response)); } $response["success"] = 1; $response["message"] = "news added!"; echo json_encode($response); ?>
in android project : java file :
public class addnews extends dialog { private activity mactivity; private edittext mnewstitleet; private edittext mnewset; private button mpublishbtn; public string username; jsonparser jsonparser = new jsonparser(); private string add_url = "http://yazanyazan3.esy.es/newssite/addnews.php"; public addnews(activity mactivity) { super(mactivity); this.mactivity = mactivity; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_addnews); mnewstitleet = (edittext) findviewbyid(r.id.title_news); mnewset = (edittext) findviewbyid(r.id.news_box); mpublishbtn = (button) findviewbyid(r.id.publish_btn); bundle use =getintent().getextras(); username = use.getstring("name"); mpublishbtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { attempadding(); } }); } private void attempadding() { if (!mnewstitleet.gettext().tostring().equals("") && !mnewset.gettext().tostring().equals("")) { new addnewstask().execute(); } else { toast.maketext(mactivity.getapplicationcontext(), "all fields requires", toast.length_long).show(); } } public intent getintent() { return mactivity.getintent(); } private class addnewstask extends asynctask<void, void, boolean> { private progressdialog mprogressdialog; private jsonobject jsonobjectresult = null; private string error; @override protected void onpreexecute() { super.onpreexecute(); mprogressdialog = progressdialog.show(addnews.this.getcontext(), "processing...", "adding new news", false, false); } @override protected boolean doinbackground(void... params) { dateformat dateformat = new simpledateformat("yyyy-mm-dd"); date date = new date(); list<namevaluepair> pairs = new arraylist<namevaluepair>(); pairs.add(new basicnamevaluepair("news_user",username)); pairs.add(new basicnamevaluepair("news_date", dateformat.format(date).tostring())); pairs.add(new basicnamevaluepair("news_title", mnewstitleet.gettext().tostring())); pairs.add(new basicnamevaluepair("news_body", mnewset.gettext().tostring())); jsonobjectresult = jsonparser.makehttprequest(add_url+"?user="+username+"", pairs); if (jsonobjectresult == null) { error = "error in connection"; return false; } try { if (jsonobjectresult.getint("success") == 1) { error = jsonobjectresult.getstring("message"); return true; } else error = jsonobjectresult.getstring("message"); } catch (exception ex) { } return false; } @override protected void onpostexecute(boolean aboolean) { super.onpostexecute(aboolean); mprogressdialog.dismiss(); toast.maketext(mactivity.getapplicationcontext(), error, toast.length_long).show(); dismiss(); } } }
in database, choose utf8 . , tried solutions did not work . please help.
hope these tips :
try adding static text database in addition file (do not read request) can divide problem 2 parts , can see if problem android client php server or php server database :
<?php /* please type arabic string(salam) own keyboard , not copy version because used persian keyboard. */ $arabic_string = 'سلام'; file_put_contents("arabic_file", $arabic_string); $query_params = array( ':news_date' => $_post['news_date'], ':news_title' => $_post['news_title'], ':news_body' => $arabic_string ); ?>
now check both "arabic_file" content , database.
-if file not ok(contained gibberish) problem php server(like maybe not support arabic language,...).
-if database not ok(gibberish again) maybe problem database or way php's pdo inserting data database, , should specify encoding connection :
$connection_str = "mysql:host=$host;dbname=$db;charset=utf8";
-if ok problem way arabic strings coming android device.
Comments
Post a Comment