从php返回特定数量的值到Android。

时间:2022-07-25 21:15:55

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:

我的android代码遇到了麻烦。我想在Android中画一个图。我想用PHP脚本连接到MySQL。我试图向脚本发送一些参数,但它总是返回null。PHP代码:

<?

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:

和Android代码:

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());
}

2 个解决方案

#1


1  

Combined awnser of the comments:

综合评论:

1: change to mysqli or pdo (see Advantages Of MySQLi over MySQL)

1:改为mysqli或pdo(见mysqli优于MySQL的优点)

2: prevent sql injection (see halfway down https://*.com/tags/php/info)

2:防止sql注入(参见半路上的https://*.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.

如果它不能工作,请在常规浏览器中测试您的页面,以确保PHP部分能够正常工作。还可以添加一些var_dump()来检查值。

#2


0  

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. 尝试使用普通浏览器连接到php页面。如果它起作用,您就知道错误在java代码中。如果它不工作,您可以暂时不使用java代码,而是专注于让php页面工作。
  3. Hard code valid values for Datum and Vreme and see if the php-code works when leaving the POST-part out of the equation.
  4. 对Datum和Vreme的有效值进行硬编码,并查看php代码在将post部分从等式中删除时是否有效。
  5. Try your query in mysql to see that it does what you expect before putting into php.
  6. 在mysql中尝试查询,看看它在进入php之前做了什么。
  7. Enable the general query log to see what php sends to mysql.
  8. 启用常规查询日志,查看php发送到mysql的内容。

This way you will pin point the error.

这样你就可以把错误的地方固定住。

#1


1  

Combined awnser of the comments:

综合评论:

1: change to mysqli or pdo (see Advantages Of MySQLi over MySQL)

1:改为mysqli或pdo(见mysqli优于MySQL的优点)

2: prevent sql injection (see halfway down https://*.com/tags/php/info)

2:防止sql注入(参见半路上的https://*.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.

如果它不能工作,请在常规浏览器中测试您的页面,以确保PHP部分能够正常工作。还可以添加一些var_dump()来检查值。

#2


0  

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. 尝试使用普通浏览器连接到php页面。如果它起作用,您就知道错误在java代码中。如果它不工作,您可以暂时不使用java代码,而是专注于让php页面工作。
  3. Hard code valid values for Datum and Vreme and see if the php-code works when leaving the POST-part out of the equation.
  4. 对Datum和Vreme的有效值进行硬编码,并查看php代码在将post部分从等式中删除时是否有效。
  5. Try your query in mysql to see that it does what you expect before putting into php.
  6. 在mysql中尝试查询,看看它在进入php之前做了什么。
  7. Enable the general query log to see what php sends to mysql.
  8. 启用常规查询日志,查看php发送到mysql的内容。

This way you will pin point the error.

这样你就可以把错误的地方固定住。