Reputation: 33
I'm experiencing troubles with my android code. I'm trying to plot a graph within Android. I want to connect to MySQL base using PHP script. I'm trying to send some parameters to script, but it keeps returning null. PHP code:
<?
mysql_connect(...);
mysql_select_db("temperature");
$Vreme = $_POST['Vreme'];
$Datum = $_POST['Datum'];
$q = mysql_query("SELECT * FROM temperature WHERE
((datum > $Datum) || (datum = $Datum)) && (vreme > $Vreme) ");
while($e = mysql_fetch_assoc($q))
$output[] = $e;
print(json_encode($output));
mysql_close();
?>
And Android code:
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("Vreme",s1));
nameValuePairs.add(new BasicNameValuePair("Datum",s2));
InputStream is = null;
try {
String adresa="http://senzori.open.telekom.rs/grafik.php";
HttpPost httppost = new HttpPost(adresa);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch(Exception e) {
Log.e("log_tag", "Error converting result "+e.toString());
}
Upvotes: 2
Views: 246
Reputation: 14173
Combined awnser of the comments:
1: change to mysqli or pdo (see Advantages Of MySQLi over MySQL)
2: prevent sql injection (see halfway down https://stackoverflow.com/tags/php/info)
Also when looking at your code you dont use quotes around your date (and vreme if its not numeric). Try
"SELECT * FROM temperature WHERE (datum>='$Datum' && vreme>'$Vreme')"
If it doesnt work test your page in a regular browser to make sure the PHP part works. Also you could add some var_dump()
to check values.
Upvotes: 1
Reputation: 40021
You should try to debug the individual parts individually.
This way you will pin point the error.
Upvotes: 0