title: Haversine Fomula with MySQL get nearby locations
body: <p>I'm trying to use the haversine formula in my PHP/MySQL to get nearby locations to where I am</p>
  
  <p>I'm using AJAX to get only nearby markers on my google map.</p>
  
  <p>What I know is that my query isn't returning anything.</p>
  
  <p>I know that there is a location within the distance of where I'm looking.</p>
  
  <p>Basically, what's wrong with my code using the haversine formula?</p>
  
  <p>EDIT - Changed a ton to follow tutorial from <a href="http://www.snipe.net/2008/12/ip-geolocation-radius-search-in-php/" rel="nofollow" title="Here">here</a>. The error is still the same.</p>
  
  <p>PHP</p>
  
  <pre><code>$lati = 40.78405877579076;
  $longi = -73.94800172194275;
  
  class RadiusCheck
  {
      var $maxLat;
      var $minLat;
      var $maxLong;
      var $minLong;
  
      function RadiusCheck($Latitude, $Longitude, $Miles)
      {
          global $maxLat,$minLat,$maxLong,$minLong;
          $EQUATOR_LAT_MILE = 69.172;
          $maxLat = $Latitude + $Miles / $EQUATOR_LAT_MILE;
          $minLat = $Latitude - ($maxLat - $Latitude);
          $maxLong = $Longitude + $Miles / (cos($minLat * M_PI / 180) * $EQUATOR_LAT_MILE);
          $minLong = $Longitude - ($maxLong - $Longitude);
      }
  
      function MaxLatitude()
      {
          return $GLOBALS["maxLat"];
      }
      function MinLatitude()
      {
          return $GLOBALS["minLat"];
      }
      function MaxLongitude()
      {
          return $GLOBALS["maxLong"];
      }
      function MinLongitude()
      {
          return $GLOBALS["minLong"];
      }
  }
  
  class DistanceCheck
  {
      function DistanceCheck()
      {
  
      }
  
      function Calculate($dblLat1, $dblLong1, $dblLat2, $dblLong2)
      {
          $EARTH_RADIUS_MILES = 3963;
          $dist = 0;
  
          //convert degrees to radians
          $dblLat1 = $dblLat1 * M_PI / 180;
          $dblLong1 = $dblLong1 * M_PI / 180;
          $dblLat2 = $dblLat2 * M_PI / 180;
          $dblLong2 = $dblLong2 * M_PI / 180;
  
          if ($dblLat1 != $dblLat2 || $dblLong1 != $dblLong2)
          {
              //the two points are not the same
              $dist = sin($dblLat1) * sin($dblLat2) + cos($dblLat1) * cos($dblLat2) * cos($dblLong2 - $dblLong1);
              $dist = $EARTH_RADIUS_MILES * (-1 * atan($dist / sqrt(1 - $dist * $dist)) + M_PI / 2);
          }
  
          return $dist;
      }
  }
  
  // set a default number of miles to search within
  $Miles = '2';
  
  // set the user's latitude and longitude as the one to search against
  $Latitude = $lati;
  $Longitude = $longi;
  
  $zcdRadius = new RadiusCheck($Latitude,$Longitude,$Miles);
  $minLat = $zcdRadius-&gt;MinLatitude();
  $maxLat = $zcdRadius-&gt;MaxLatitude();
  $minLong = $zcdRadius-&gt;MinLongitude();
  $maxLong = $zcdRadius-&gt;MaxLongitude();
  
  $sql = "SELECT `uname`, `it`, `name`, SQRT((((69.1*(latitude-$Latitude))*(69.1*(latitude-$Latitude)))+((53*(longitude-$Longitude))*(53*(longitude-$Longitude))))) AS calc FROM stores WHERE latitude &gt;= '$minLat' AND latitude &lt;= '$maxLat' AND longitude &gt;= '$minLong' AND longitude &lt;= '$maxLong'";
  
  $get_data = mysqli_query($conn, $sql);
  
  while($row = mysqli_fetch_assoc($get_data))
  {
      // calculate the number of miles away the result is
      $zcdDistance = new DistanceCheck;
      $Distance = $zcdDistance-&gt;Calculate($Latitude, $Longitude, $row['latitude'], $row['longitude']);
  
      $lat = $row['lat'];
      $lon = $row['lon'];
      $name = $row['name'];
      $it = $row['it'];
      $uname = $row['uname'];
  
      $data["markers"][] = array
      (
          "latitude" =&gt; $lat,
          "longitude" =&gt; $lon,
          "name" =&gt; $name,
          "it" =&gt; $it,
          "uname" =&gt; $uname
      );
  }
  echo json_encode($data);
  </code></pre>
  
  <p>Error</p>
  
  <pre><code>&lt;b&gt;Warning&lt;/b&gt;:  mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given ... null
  </code></pre>