我努力学习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