title: php mysql compare long and lat, return ones under 10 miles
body: <p>Hay i want to find the distance (in miles) between 2 locations using lat and long values, and check if they are within a 10 mile radius of each other.</p>
  
  <p>When a user logs in, their lat/long values are saved in a session</p>
  
  <pre><code>$_SESSION['lat']
  $_SESSION['long']
  </code></pre>
  
  <p>I have 2 functions</p>
  
  <p>This one works out the distance in miles and returns a rounded value</p>
  
  <pre><code>function distance($lat1, $lng1, $lat2, $lng2){
      $pi80 = M_PI / 180;
      $lat1 *= $pi80;
      $lng1 *= $pi80;
      $lat2 *= $pi80;
      $lng2 *= $pi80;
      $r = 6372.797; // mean radius of Earth in km
      $dlat = $lat2 - $lat1;
      $dlng = $lng2 - $lng1;
      $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
      $c = 2 * atan2(sqrt($a), sqrt(1 - $a));
      $km = $r * $c;
      return floor($km * 0.621371192);
  }
  </code></pre>
  
  <p>This one returns a bool if the distance between the 2 sets of lat and long's is under 10.</p>
  
  <pre><code>function is_within_10_miles($lat1, $lng1, $lat2, $lng2){
      $d = distance($lat1, $lng1, $lat2, $lng2);
      if( $d &lt;= 10 ){
          return True;
      }else{
          return False;
      }
  }
  </code></pre>
  
  <p>Both functions work as expected, if i give 2 sets of lat/longs and the distance between them is say 20 miles, my is_within_10_miles() function returns false.</p>
  
  <p>Now, I have a database of 'locations' (4 fields - ID, name, lat, long).</p>
  
  <p>I want to find all locations that are within a 10 mile radius.</p>
  
  <p>Any ideas?</p>
  
  <p><strong>EDIT: I can loop through ALL the and perform the is_within_10_miles() on them like this</strong></p>
  
  <pre><code>$query = "SELECT * FROM `locations`";
  $result = mysql_query($query);
  
  while($location = mysql_fetch_assoc($result)){
  echo $location['name']." is ";
  echo distance($lat2, $lon2, $location['lat'], $location['lon']);
  echo " miles form your house, is it with a 10 mile radius? ";
  if( is_within_10_miles($lat2, $lon2, $location['lat'], $location['lon']) ){
      echo "yeah";
  }else{
      echo "no";
  }
  echo "&lt;br&gt;";
  </code></pre>
  
  <p>}</p>
  
  <p>A sample result would be</p>
  
  <pre><code>goodison park is 7 miles form your house, is it with a 10 mile radius? yeah
  </code></pre>
  
  <p>I need to somehow perform the is_within_10_miles function within my query.</p>
  
  <p>EDIT EDIT</p>
  
  <p>This legend from <a href="http://www.zcentric.com/blog/2007/03/calculate_distance_in_mysql_wi.html" rel="noreferrer">http://www.zcentric.com/blog/2007/03/calculate_distance_in_mysql_wi.html</a> came up with this...</p>
  
  <pre><code>SELECT ((ACOS(SIN($lat * PI() / 180) * SIN(lat * PI() / 180) + COS($lat * PI() / 180) * COS(lat * PI() / 180) * COS(($lon - lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS distance FROM members HAVING distance&lt;='10' ORDER BY distance ASC
  </code></pre>
  
  <p>It actually works. Problem is that i want to select * rows, rather than selecting them one by one. How do i do that?</p>