2012-01-03 53 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