title: How to find nearest location by latitude and longitude?
body: <p>This is the <code>loc_coordinate</code> table structure:</p>
  
  <p><img src="https://i.stack.imgur.com/dHTbs.png" alt="enter image description here"></p>
  
  <p>Below is the code, to fetch the nearest places from database and display the place name stored in database itself.</p>
  
  <pre><code>&lt;?php
  include("config.php");
  $lat = "3.107685";
  $lon = "101.7624521";
  
          $sql="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 loc_coordinate HAVING 'distance'&lt;='10' ORDER BY 'distance' ASC";
          $stmt =$pdo-&gt;prepare($sql);
          $stmt-&gt;execute();
  
  
          while($row = $stmt-&gt;fetch())
          {
            echo $row['place'];
          }
  
  ?&gt;
  </code></pre>
  
  <p>The error shown for this:</p>
  
  <blockquote>
    <p>Fatal error: in C:\wamp\www\mysite\by_coor.php on line 8</p>
    
    <p>PDOException: in C:\wamp\www\mysite\by_coor.php on line 8</p>
  </blockquote>
  
  <p><code>echo $sql</code> shows this:</p>
  
  <blockquote>
    <p>SELECT ((ACOS(SIN(3.107685 * PI() / 180) * SIN(lat * PI() / 180) +
    COS(3.107685 * PI() / 180) * COS(lat * PI() / 180) * COS((101.7624521
    â€“ lon) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) AS 'distance'
    FROM loc_coordinate HAVING 'distance'&lt;='10' ORDER BY 'distance' ASC</p>
  </blockquote>
  
  <p>I'm unsure why I'm getting this error. This is the site I referred to for the SQL query: <a href="http://zcentric.com/2010/03/11/calculate-distance-in-mysql-with-latitude-and-longitude/" rel="noreferrer">http://zcentric.com/2010/03/11/calculate-distance-in-mysql-with-latitude-and-longitude/</a> </p>