android通过http请求从web数据库检索数据,PHP不工作


android retrieve data from web database through http request and PHP doesn't work

我试图通过Http请求从web数据库检索数据。但这行不通。只有当sql字符串包含引号时,它才会失败。否则,它工作得很好。

当我调试时,返回字符串总是这样:

Warning:  mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/28/8269928/html/test.php on line 12 null (id=830020201688)
Android侧代码:
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("strSql", "select * from user where username='test'"));

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://test.com/test.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }
    catch(Exception e) {
           System.out.println("Connectiong Error");
    }
    String js = "";
    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();
        js = sb.toString();
        System.out.println("get = " + js);
    }
    catch(Exception e) {
           System.out.println("Error converting to String");
    }

web服务器PHP代码:

$myconn=mysql_connect("68.178.139.15", "username", "password");  
mysql_select_db("dbname");
mysql_query("set names 'utf8'");
$strSql = $_REQUEST['strSql'];
$result = mysql_query($strSql, $myconn);
while($row = mysql_fetch_array($result)) {
    $output[]=$row;
}
print(json_encode($output));
mysql_close();  

solved

android的一面:URLEncoder。Encode ("select * from test where name= 'test '")

php的一面:urldecode ($ _REQUEST [' strSql ']);