我努力學習PHP代碼,我在做一個教程,我就發現androidhive(在this鏈接)沒有商品。PHP代碼返回基於過濾
當我要求的產品數據與過濾器,我被卡住。 這是androidhive做代碼:
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("pid", pid));
和PHP:
if (isset($_GET["pid"])) {
$pid = $_GET['pid'];
// get a product from products table
$result = mysql_query("SELECT *FROM products WHERE pid = $pid");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
$product = array();
$product["pid"] = $result["pid"];
$product["username"] = $result["username"];
...
// success
$response["success"] = 1;
// user node
$response["product"] = array();
array_push($response["product"], $product);
// echoing JSON response
echo json_encode($response);
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found 1";
// echo no users JSON
echo json_encode($response);
}
} else {
// no product found
$response["success"] = 0;
$response["message"] = "No product found 2";
// 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);
}
雖然我基本上這樣做:
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("username", "a"));
而在PHP:
if (isset($_GET["username"])) {
$username = $_GET['username'];
// get a product from products table
$result = mysql_query("SELECT *FROM products WHERE username = $username");
所以我在以不同的價值過濾產品。
然而,這回我「沒有找到產品2」,所以我的結果是空的,我猜。 當我運行原來的代碼,它完美的作品。
任何人都可以幫助我嗎?提前致謝!
謝謝查詢VARCHAR。標記爲已接受,因爲你看起來我猜的來源。我是否需要將字符串值加上引號? – Matteo 2015-01-04 12:47:25
是的,你必須爲string,text和varchar類型的數據加上引號。 – BeingMIAkashs 2015-01-04 12:49:05