0

试图让我的 PHP 脚本返回一些 SQL 表查询。这是我现在的脚本:

<?php

define("DB_HOST", "localhost");
define("DB_USER", "*");
define("DB_PASSWORD", "*");
define("DB_DATABASE", "*");

mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db(DB_DATABASE);


if (isset($_POST['tag']) && $_POST['tag'] != '') {
    $tag = $_POST['tag'];
    echo $tag;
if ($tag == 'question') {
    $category = $_POST['category'];
    $response=mysql_query("select * from QUESTIONS where CATEGORY like '$category'");
    return $category; //just doing this, rather than $response to see if it works
}
}
?>

这是与之关联的 Android 代码:

public JSONObject getQuestionsJSONFromUrl(String url, List<NameValuePair> params) {

    // Making HTTP request
try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            Log.v("while", line);
            sb.append(line + "\n");
            //Log.v("err", line);
        }
        is.close();

以及调用 getQuestionsJSON 的方法...:

private static String question_tag = "question";
public JSONObject getQuestions(String category) {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", question_tag));
    params.add(new BasicNameValuePair("category", category));
    //JSONObject json;
    JSONObject questionsList = jsonParser.getQuestionsJSONFromUrl(questionURL, params);
    //return json
    return null;
}

这是我在 getQuestionsJSON...() 方法中的 Log.v() 的 LogCat:

04-04 20:41:58.721: V/while(933): question

所以我真的不明白为什么这会返回“问题”而不是运行 getQuestions() 时传递的字符串?

4

1 回答 1

2

在 PHP 文件中,你有

echo $tag;

它是对请求的响应。

这应该返回 mysql 响应:

if (isset($_POST['tag']) && $_POST['tag'] != '') {
    $tag = $_POST['tag'];
    if ($tag == 'question') {
        $category = $_POST['category'];
        $response=mysql_query("select * from QUESTIONS where CATEGORY like '$category'");

        $rows = array();
        while($r = mysql_fetch_assoc($response)) {
            $rows[] = $r;
        }
        print json_encode($rows);
    }
}
于 2012-04-04T20:59:58.340 回答