【问题标题】:PHP MySQL: Query with Less Than AND Greater ThanPHP MySQL:查询小于和大于
【发布时间】:2012-07-20 22:04:13
【问题描述】:

我的 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 出了什么问题?

【问题讨论】:

  • lat的数据类型是什么?
  • 将错误添加到您的问题中会有很大帮助。 echo mysql_error(); 在你执行 mysql_query($sql); 之后。
  • 请不要将mysql_* 函数用于新代码。它们不再维护,社区已经开始deprecation process。看到red box?相反,您应该了解prepared statements 并使用PDOMySQLi。如果您不能决定,this article 将帮助您选择。如果你想学习,here is good PDO tutorial.
  • mysql_error() 仍然返回一个空白页
  • 你真的有数据可以匹配你的查询吗?

标签: php mysql


【解决方案1】:

使用 MySQl 的 BETWEEN从不 使用引号来比较数字。于是查询变为:

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

【讨论】:

  • 感谢这似乎是正确的,shop_id 实际上是 VARCHAR,但是我仍然得到一个空白的白页。我认为数学计算有问题
  • @user1417302 Here is the code I tried to conver to PHP:。从哪里转换代码?为什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-12
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多