title: PHP/MySQL: Select locations close to a given location from DB
body: <p>In PHP, I have the following code for calculating the distance between two locations:</p>
  
  <pre><code>&lt;?php
  function distance($lat1, $long1, $lat2, $long2) {
      // DEGREE TO RADIAN
      $latitude1 = $lat1/180*pi();
      $longitude1 = $long1/180*pi();
      $latitude2 = $lat2/180*pi();
      $longitude2 = $long2/180*pi();
      // FORMULA: e = ARCCOS ( SIN(Latitude1) * SIN(Latitude2) + COS(Latitude1) * COS(Latitude2) * COS(Longitude2-Longitude1) ) * EARTH_RADIUS
      $distance = acos(sin($latitude1)*sin($latitude2)+cos($latitude1)*cos($latitude2)*cos($longitude2-$longitude1))*6371;
      return $distance;
  }
  echo distance(9.9921962, 53.5534074, 9.1807688, 48.7771056); // Hamburg, DE - Stuttgart, DE
  ?&gt;
  </code></pre>
  
  <p>But now, I want to select locations close to a given location via PHP from my MySQL database:</p>
  
  <ul>
  <li>The user enters his hometown</li>
  <li>My script gets the latitude/longitude values via the Google API</li>
  <li>In my database, I have about 200 locations with a field for the latitude value and a field for the longitude value</li>
  <li>I need a code for PHP and MySQL to select the 10 locations which are closest to the user's hometown</li>
  </ul>
  
  <p>I hope you can help me. Thanks in advance!</p>