我的 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 的 BETWEEN
和 NEVER 使用引号比较数字。所以查询变成:
$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
列是自动递增的数字。
关于PHP MySQL : Query with Less Than AND Greater Than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580608/