繁体   English   中英

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

解决了

android端:URLEncoder.encode(“ select * from test where name = \\'test \\'”)

php端:urldecode($ _ REQUEST ['strSql']);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM