PHP MySQL : Query with Less Than AND Greater Than

标签 php mysql

我的 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列是自动递增的数字。

关于PHP MySQL : Query with Less Than AND Greater Than,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11580608/

相关文章:

php - htaccess : Rewrite URL issue for query string in parameter

php - 案例中的 Foreach 循环

php - 如何在php中比较数组和数据库

php - 查找单元测试中的失败模式

mysql - 没有for update的select语句什么时候会导致锁?

php - PHP检查用户名是否存在

sql - 查找 "missing"行

PHP 从 mySQL 数据库查询音频文件

php - 如何从 Magento 外部访问 Magento 用户的 session ?

javascript - 从 Node js中的MYSQL元素集中选择特定元素