PHP MySQL:使用小于和大于进行查询


PHP MySQL: Query with Less Than AND Greater Than

我的PHP文件中有以下mysql查询:

$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'";

由于某些原因,查询未成功运行。上述查询有效吗?感谢

编辑:

$long_min和$long_max的计算有问题,因为当它们被注释掉时,它可以正常工作

以下是我尝试转换为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);

我的PHP出了什么问题?

使用MySQl的BETWEENNEVER使用引号比较数字。因此查询变成:

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

其中,我认为shop_id列是自动递增的数字。