php小于等于符号,PHP MySQL:小于等于大于的查询

I have the following mysql query in my PHP file:

$shopID = mysql_real_escape_string($_GET['shop_id']);

$latitudeCenter = mysql_real_escape_string($_GET['lat_center']);

$longtitudeCenter = mysql_real_escape_string($_GET['lng_center']);

$lat_min = $latitudeCenter - 0.045;

$lat_max = $latitudeCenter + 0.045;

$long_min = $longtitudeCenter - (0.045 / cos($latitudeCenter*M_PI/180);

$long_max = $longtitudeCenter + (0.045 / cos($latitudeCenter*M_PI/180);

$sql = "SELECT * FROM shops WHERE shop_id = '$shopID' AND lat >= '$lat_min' AND lat <= '$lat_max' AND lng >= '$long_min' AND lng <= '$long_max'";

For some reason the query is not running successfully. Is the above query valid?

Thanks

EDIT:

There is something wrong with the $long_min and $long_max calculations as when they are commented out, it works ok.

Here is the code I tried to conver to PHP:

lat_min = lat_center - 0.045;

lat_max = lat_center + 0.045;

long_min = long_center - (0.045 / Math.cos(lat_center*Math.PI/180);

long_max = long_center + (0.045 / Math.cos(lat_center*Math.PI/180);

What is wrong with my PHP?

解决方案

Use the MySQl's BETWEEN and NEVER use quotes for comparing numbers. So the query becomes:

$sql = "SELECT *

FROM shops

WHERE shop_id = $shopID

AND lat BETWEEN $lat_min AND $lat_max

AND lng BETWEEN $long_min AND $long_max";

Where, I have considered that shop_id column is auto-incremental number.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值