0

Okay so I need to a retrieve specific field from an online database, based on some parameters.
The parameters have to be passed from the Android application to the PHP script (I have that working for one parameter, but it fails for multiple).

PHP Script

<?php error_reporting (E_ALL ^ E_NOTICE); ?>
<?php 
$username = "root";
$password = "";
$host = "localhost";
$dbname = "db.projectblue";

$chapter = mysql_real_escape_string($_POST['column']);
$id = mysql_real_escape_string($_POST['id']);

mysql_connect($host, $username, $password);
mysql_select_db($dbname);

$q=mysql_query("SELECT '$column' FROM tblphysics WHERE id='$id'");
while($e=mysql_fetch_assoc($q))
    $output[]=$e;

print(json_encode($output));

mysql_close();

?>

As you see the SQL query has to be created dynamically based on the input. It works if i replace the $column variable with a definite column name such as version or chapter, but wont work with the variable.

This is my JAVA code

private JSONObject getJSONObject(int id, String column) {
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
    nameValuePair.add(new BasicNameValuePair("column", column));
    nameValuePair.add(new BasicNameValuePair("id", Integer.toString(id)));


    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(PARSER_URL);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    } catch (Exception e) {
        Log.e(LOG_TAG_JSON, "Error in http connection " + e.toString());
    }
    // convert response to string
    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();

        result = sb.toString();
    } catch (Exception e) {
        Log.e("log_tag", "Error converting result " + e.toString());
    }
    try {
        JSONArray jArray = new JSONArray(result);
        JSONObject json_data = jArray.getJSONObject(0);

        return json_data;

    } catch (JSONException e) {
        Log.e(LOG_TAG_JSON, "Error parsing data " + e.toString());
    }
    return null;

}

And I am calling the private method like so

public String getChapter(int id) throws JSONException {
    JSONObject object = getJSONObject(id, "chapter");
    String iChapter = object.optString("chapter");

    return iChapter;
}

When I call this public method with the two variables in php script the output (log from another method) is this:

Version (this is a column name in db) : 0 (should be 5)
Chapter (this is a column name in db) : chapter (should be newton)

My question is:
Why wont the php script work with more than one variable ?
Is my NameValuePair setup correctly?
Is it even alowed to create the SQL query with dynamic column name and/or table name?

Edit: The fix was so simple I didn't even see it. I had $column in query instead of $chapter and the variable in query has to be ".$chapter."

4

1 回答 1

0

Hint Hint: before the query why dont u try to echo $coulmn and see the value stored inside $column.

You can refer this example and get some of your doubts cleared http://sarangasl.blogspot.com/2011/06/android-login-screen-using-httpclient.html

于 2013-07-18T02:48:12.793 回答