0

我试图通过 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)

安卓端代码:

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

网络服务器 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();  
4

1 回答 1

0

solved

android side: URLEncoder.encode("select * from test where name=\'test\'")

php side: urldecode($_REQUEST['strSql']);

于 2013-03-04T18:01:33.747 回答