Мой запрос SQL через PHP работает, когда я жестко кодирую значение запроса, но не когда я использую $_GET для разбора URL
Когда я жестко закодирую значение моего поиска в своем запросе, как показано ниже (в запросе 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);
}
?>
Тем не менее, когда я пытаюсь запросить мой SQL через мой код Android и PHP, как показано ниже,! Empty($result) возвращает false, возвращая мое сообщение "empty results".
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;
}
}
Нужно ли преобразовывать переменные $user в переменную $user в Java или PHP?