Brana
Brana

Reputation: 33

Returning specific number of values from php to Android

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

Answers (2)

Hugo Delsing
Hugo Delsing

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

Andreas Wederbrand
Andreas Wederbrand

Reputation: 40021

You should try to debug the individual parts individually.

  1. Try to connect to your php-page using a normal browser. If it works you know the error is in your java-code. If it doesn't work you could leave the java-code alone for now and focus on making the php-page work.
  2. Hard code valid values for Datum and Vreme and see if the php-code works when leaving the POST-part out of the equation.
  3. Try your query in mysql to see that it does what you expect before putting into php.
  4. Enable the general query log to see what php sends to mysql.

This way you will pin point the error.

Upvotes: 0

Related Questions