Reputation: 89
I need to read data from an external database with hundreds of items. What I did so far is that I wrote the php query which returns all the items, so for i have done
php:
$db_host = "host";
$db_uid = "username";
$db_pass = "password";
$db_name = "person";
$db_con = mysql_connect($db_host,$db_uid,$db_pass) or die('could not connect');
mysql_select_db($db_name);
$sql = "SELECT * FROM employee ";
$result = mysql_query($sql);
while($row=mysql_fetch_assoc($result))
$output[]=$row;
print(json_encode($output));
mysql_close();
android:
String url = "http://localhost/index.php";
@Override
public void onCreate(Bundle savedInstanceState) {
/*
* StrictMode is most commonly used to catch accidental disk or network
* access on the application's main thread
*/
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.hospital);
byear = (EditText) findViewById(R.id.editText1);
submit = (Button) findViewById(R.id.submitbutton);
tv = (TextView) findViewById(R.id.showresult);
// define the action when user clicks on submit button
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// declare parameters that are passed to PHP script i.e. the
// name "birthyear" and its value submitted by user
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
data = byear.getText().toString();
// define the parameter
postParameters.add(new BasicNameValuePair("data", data));
String response = null;
// call executeHttpPost method passing necessary parameters
try {
response = CustomHttpClient.executeHttpPost(
url,
postParameters);
// store the result returned by PHP script that runs
// MySQL query
String result = response.toString();
// parse json data
try {
returnString = "";
JSONArray jArray = new JSONArray(result);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag", "id: " + json_data.getInt("id")
+ ", name: " + json_data.getString("name")
);
// Get an output to the screen
returnString += "\n" + "Name ="
+ json_data.getString("name") + "\n"
+ "Contact number = "
+ json_data.getInt("contact") + "\n"
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
try {
tv.setText(returnString);
} catch (Exception e) {
Log.e("log_tag", "Error in Display!" + e.toString());
;
}
} catch (Exception e) {
Log.e("log_tag",
"Error in http connection!!" + e.toString());
}
}
});
}
}
My question is how can I pass the data from edit text android to php query so the query will only return the proper items only
Upvotes: 0
Views: 1979
Reputation: 300
To send data to php use postParameters :
postParameters.add(new BasicNameValuePairs("id",data));
In your php script add
if(isset($_POST['id']){
var id = $_POST['id'];
// do db operation here
}
with id = your data id and data = your data.
Upvotes: 1
Reputation: 5595
First you have to write respective php script Then you can use this:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://Your Url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("uid",username));
nameValuePairs.add(new BasicNameValuePair("filename",filename));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Make sure that the field name in your table in server side is "uid" and "filename" respectively. For more information see this.
Upvotes: 0
Reputation: 3791
$var_data=$_REQUEST['data'];
print($var_data);
use above 2 lines in your php code at line number 1 so it will print android data associated with key "data"
postParameters.add(new BasicNameValuePair("data", data));
Upvotes: 0