我想在我的mysql語句中使用變量,以便它們將根據輸入而改變。例如,如果用戶沒有輸入大小,則該部分語句不會顯示。對不起,我解釋不好。但是當我運行下面的代碼時,出現錯誤,但是如果我刪除了「動態」AND語句,代碼就可以工作。我無法弄清楚我做錯了什麼。和想法。我將如何使用我的mysql查詢中的變量
if($size2 ==''){
$size_stat = '';
}else{
$size_stat = "AND size='$size2'";
}
if($re2 ==''){
$re_stat = '';
}else{
$re_stat = "AND re='$re2'";
}
if($status2 ==''){
$status_stat = '';
}else{
$status_stat = "AND status='$status2' ";
}
$result2 = mysql_query("SELECT
lat,lng,id,re,re_num,name,city,state,zip,address,status,category,size,
(3959 * acos(cos(radians($lat2))
* cos(radians(lat))
* cos(radians(lng) - radians($long2))
+ sin(radians($lat2))
* sin(radians(lat))
)
) AS distance
FROM
locations
WHERE
(state='PA' OR state='NY')
.$re_stat.
$size_stat.
$status_stat.
HAVING
distance < 300
ORDER BY
distance
LIMIT
0 , 50 ");
while($array = mysql_fetch_assoc($result2)){
$array_json[] = $array;
}
echo json_encode($array_json);
缺少whitesapec猜測。例如$ status_stat =「AND status ='$ status2'」; – 2012-03-16 15:02:28