我通过PHP的SQL查询在我对查询值进行硬编码时起作用,但在我使用$ _GET来解析URL时则不行

时间:2015-06-16 22:57:48

标签: php android mysql sql-server http

当我将搜索值硬编码到我的请求中时(在SQL查询中),一切运行顺利,我检索到正确的结果。

add.php
<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */

// array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["user"])) {
    $user = $_GET['user'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM prediction 
                           WHERE user = 'codohert'
                           ORDER BY prediction_made DESC
                           LIMIT 1");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $prediction = array();
            $prediction["user"] = $result["user"];
            $prediction["occupancy_prediction"] = $result["occupancy_prediction"];
            $prediction["prediction_made"] = $result["prediction_made"];
            // success
            $response["success"] = 1;

            // user node
            $response["prediction"] = array();

            array_push($response["prediction"], $prediction);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "Not enough rows";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "Empty results";
        $response["var"] = $user;

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

但是,当我尝试通过我的Android和PHP代码查询我的SQL时,如下所示,!empty($ result)返回false,返回我的“空结果”消息。

if (isset($_GET["user"])) {
    $user = $_GET['user'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM prediction 
                           WHERE user = $user
                           ORDER BY prediction_made DESC
                           LIMIT 1");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $prediction = array();
            $prediction["user"] = $result["user"];
            $prediction["occupancy_prediction"] = $result["occupancy_prediction"];
            $prediction["prediction_made"] = $result["prediction_made"];
            // success
            $response["success"] = 1;

            // user node
            $response["prediction"] = array();

            array_push($response["prediction"], $prediction);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "Not enough rows";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "Empty results";
        $response["var"] = $user;

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

Java代码:

protected String doInBackground(String... param) {

        Log.i("update", "started");
        /*
         * SharedPreferences sharedPref = context.getSharedPreferences("user",
         * Context.MODE_PRIVATE); user = sharedPref.getString("name",
         * "codohert");
         */

        user = "codohert";

        try {

            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("user", user));

            // getting product details by making HTTP request
            // Note that product details url will use GET request
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .permitNetwork().build());
            JSONObject json = jsonParser.makeHttpRequest(url_prediction, "GET",
                    params);

            // check your log for json response
            Log.i("Prediction Details", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            if (success == 1) {
                Log.i("update", "success");
                // successfully received product details
                JSONArray predictionObj = json.getJSONArray(TAG_PREDICTION); // JSON
                                                                                // Array

                // get first product object from JSON Array
                JSONObject prediction = predictionObj.getJSONObject(0);

                String predict = prediction.getString(TAG_OCCUPANCY_PREDICTION);
                String shour = predict.substring(11, 12);
                String smin = predict.substring(14, 15);
                Log.i("results", shour);
                Log.i("results", smin);
                Integer hour = Integer.parseInt(shour);
                Integer minute = Integer.parseInt(smin);

                SetTime(hour, minute);
                return null;

            } else {
                Log.i("update", "failed");
                return null;
            }
        } catch (JSONException e) {
            Log.i("update", "catch");
            e.printStackTrace();
            return null;
        }
    }

我是否需要在Java或PHP中将$ user变量转换为其他类型?

0 个答案:

没有答案