Natasha
Natasha

Reputation: 259

selecting points within the specific distances

i have many deals in my database. i want to seleect some specific deals from the database by sending arguments "lat" "lon" and the radius.. this query isnt working what is wront with it..

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

Upvotes: 0

Views: 407

Answers (2)

user319198
user319198

Reputation:

Small change required replace second where with and.We cant use two where clause here. aSAlso two from clause are not allowed.

Try with below query.

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

Upvotes: 2

James
James

Reputation: 3805

It's not working because you have 2 "WHERE" statements. You can probably just change the second "WHERE" to an "AND".

Upvotes: 3

Related Questions