2012-01-03 72 views
0

我在我的数据库中有很多优惠。我想通过发送参数“纬度”,“经度”和半径。要seleect从数据库中的一些特定的交易 此查询心不是工作是什么wront它..选择特定距离内的点

require('includes/connection.php'); // defines $dsn, $username, $password  
    $lat = $_GET['lat']; // latitude of centre of bounding circle in degrees 
    $lon = $_GET['lon']; // longitude of centre of bounding circle in degrees 
    $rad = $_GET['rad']; // radius of bounding circle in kilometers 
    $R = 6371; // earth's radius, km 
    // first-cut bounding box (in degrees) 
    $maxLat = $lat + rad2deg($rad/$R); 
    $minLat = $lat - rad2deg($rad/$R); 
    // compensate for degrees longitude getting smaller with increasing latitude 
    $maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat))); 
    $minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat))); 

    // convert origin of filter circle to radians 
    $lat = deg2rad($lat); 
    $lon = deg2rad($lon); 
    $sql = " Select * From From deals 
     Where lat>$minLat And lat<$maxLat 
     And lon>$minLon And lon<$maxLon 
    Where acos(sin($lat)*sin(radians(lat)) + cos($lat)*cos(radians(lat))*cos(radians(lon)-$lon))*$R < $rad"; 
echo "Query =".$sql; 
    $points = mysql_query($sql); 
+3

你是什么意思的“不工作”。它没有提供任何结果,它是否给出错误消息...?请更具体一点。你可以马上尝试打印sql查询字符串并通过例如执行结果语句。 phpMyAdmin的? – codeling 2012-01-03 14:28:37

+0

你不能只在查询中用$来传递变量,你必须使用sprintf,连接或使用类似的解决方案。请参阅http://php.net/manual/en/function.mysql-query.php – Viruzzo 2012-01-03 14:30:39

+0

@Viruzzo中的示例:实际上应该按照我所知的方式工作(请参阅http://www.brainbell.com/tutors /php/php_mysql/Variable_substitution.html) – codeling 2012-01-03 14:32:30

回答

2

小的变化需要进行更换第二whereand。我们不能在这里使用两个where条款。 aSalso from子句不允许。

请尝试以下查询。

Select * From deals 
Where lat>$minLat 
And lat<$maxLat 
And lon>$minLon 
And lon<$maxLon 
and acos(sin($lat)*sin(radians(lat))+cos($lat)*cos(radians(lat))*cos(radians(lon)-$lon))*$R < $rad";` 
3

它不工作,因为你有2 “WHERE”语句。您可以将第二个“WHERE”更改为“AND”。

+0

两个也是不允许的。 – 2012-01-03 16:01:02