PHP MySQL:小于等于大于的查询 [英] PHP MySQL: Query with Less Than AND Greater Than

查看:1088
本文介绍了PHP MySQL:小于等于大于的查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的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

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

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

这是我尝试转换为PHP的代码:

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);

我的PHP有什么问题?

What is wrong with my PHP?

推荐答案

使用MySQl的BETWEEN从不使用引号来比较数字.因此查询变为:

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

在这里,我认为shop_id列是自动递增的数字.

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

这篇关于PHP MySQL:小于等于大于的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆