我的PHP回复了错误的信息?

时间:2012-04-04 20:50:51

标签: php android sql

尝试让我的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()时传递的String?

1 个答案:

答案 0 :(得分: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);
    }
}