Davek804
Davek804

Reputation: 2804

My PHP is returning the wrong information?

Trying to have my PHP script return some SQL table queries. Here's my script as it stands right now:

<?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
}
}
?>

And here's the Android code that's associated with it:

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

And the method that calls 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;
}

And here's the LogCat of the Log.v() I have in the getQuestionsJSON...() method:

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

So I don't really see why this is returning 'question' rather than the String that is passed when I run getQuestions()?

Upvotes: 0

Views: 199

Answers (1)

Jarosław Gomułka
Jarosław Gomułka

Reputation: 4995

In PHP file you have

echo $tag;

And it is response to request.

This should return mysql response:

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

Upvotes: 2

Related Questions