php - How to recieve data from database back into android -
i have created webservice called "login.php" send id , password information android. webservice catches id , password. need compare id , password ones present in database , check whether exist or not. if do, need send "okay message" android can start new intent. if id , password not exist, want display error. below code.
login.java
httppost httppost = new httppost("http://abc.com/webservice/login.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("userid", et1.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("pass", et2.gettext().tostring())); httppost.setentity(new urlencodedformentity(namevaluepairs)); client.execute(httppost); log.d("valueeeeeeeeeeee", et6.gettext().tostring()); } catch (clientprotocolexception e) { // todo auto-generated catch block log.d("exppppppp", "msg"); } catch (ioexception e) { // todo auto-generated catch block log.d("exppppppp", "msg"); }
login.php:
<?php $host = "localhost"; $user = "user"; $pass = "pass"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<br>"); $userid = $_post['userid']; $pass = $_post['pass']; $db_select=mysql_select_db("my_db"); if(!$db_select){ die(mysql_error()); echo "error"; }
what query should run here check database against specific id , password recieved , send "okay message" android app. thanks
you can try this:
java:
httppost httppost = new httppost("http://abc.com/webservice/login.php"); try { list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2); namevaluepairs.add(new basicnamevaluepair("userid", et1.gettext().tostring())); namevaluepairs.add(new basicnamevaluepair("pass", et2.gettext().tostring())); httppost.setentity(new urlencodedformentity(namevaluepairs)); //this piece of code should trick httpresponse response = client.execute(httppost); httpentity respentity = response.getentity(); if (respentity != null) { // entityutils reponse content string content = entityutils.tostring(respentity); } log.d("valueeeeeeeeeeee", et6.gettext().tostring()); } catch (clientprotocolexception e) { // todo auto-generated catch block log.d("exppppppp", "msg"); } catch (ioexception e) { // todo auto-generated catch block log.d("exppppppp", "msg"); }
php:
<?php $host = "localhost"; $user = "user"; $pass = "pass"; $connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<br>"); $userid = mysql_real_escape_string($_post['userid']); $pass = mysql_real_escape_string($_post['pass']); $db_select=mysql_select_db("my_db"); if(!$db_select){ die(mysql_error()); echo "error"; } $query = "select count(1) count_users user_table user_field = '".$userid."' , pass_field ='".$pass."'"; $result = mysql_query($query); $row = mysql_fetch_assoc($result); if($row['count_users']>0) { echo "okey"; } else { echo "not found"; } ?>
ps: please dont use mysql_extension, go mysqli or pdo instead.
Comments
Post a Comment