My PHP is returning the wrong information?

Go To StackoverFlow.com

0

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

2012-04-04 20:50
by Davek804
echo $tag; returns your 'question - marcinj 2012-04-04 21:01


2

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);
    }
}
2012-04-04 20:59
by Jarosław Gomułka
This helped, but it didn't solve it. Now the Log returns;

04-04 21:11:32.791: V/while(1188): Resource id #2.

Here's what the output looks like in a simple query: {"category":"elections","id":"0","title":"Who will you vote for in November's Presidential election?","published":"2012-04-02","enddate":"2012-04-30","responsetype":"0"}{"category":"elections","id":"2","title":"Question title, ladies and gents","published":"2012-04-02","enddate":"2012-04-30","responsetype":"1" - Davek804 2012-04-04 21:14

I've updated my answer. Response is in JSON now - Jarosław Gomułka 2012-04-04 21:26
Thank you Jaroslaw! This will be marked correct, and I'll be putting up another post shortly. If you'd be willing to lend a bit more of a hand, I'm sure you'll get another correct response rep gain :) I'll link it here when it's posted - Davek804 2012-04-04 21:30
I'm glad I could help: - Jarosław Gomułka 2012-04-04 21:31
I am glad too Jaroslaw, here's the new post: http://stackoverflow.com/questions/10019599/parsing-json-into-jsonarra - Davek804 2012-04-04 21:35
Ads