Note that there are some explanatory texts on larger screens.

plurals
  1. POpassing variable to Haversine formula
    text
    copied!<p>using this query from google: <a href="https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=it#findnearsql" rel="nofollow">link</a></p> <p>Im trying to create a location search, returning all towns within a set distance. I want people to be able to input the town in a form, my code find the lat + long of the town name, and then query my database to return all towns within set distance.</p> <p>to start, i find the lat + long of the inputted location:</p> <pre><code> $location = 'farnborough'; function getlatlang($location) { $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&amp;sensor=false'); $output= json_decode($geocode); return $output-&gt;results[0]-&gt;geometry-&gt;location; } $objlocation = getlatlang($location); $latitude = $objlocation-&gt;lat; $longitude = $objlocation-&gt;lng; </code></pre> <p>this works fine, i can echo the result and the values are correct.</p> <p>then using the query in the above link, i try:</p> <pre><code> $query = "SELECT town, ( 3959 * acos( cos( radians($latitude) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($longitude) ) + sin( radians($latitude) ) * sin( radians( lat ) ) ) ) AS distance FROM uk_postcode_05 HAVING distance &lt; 25 ORDER BY distance ASC LIMIT 0 , 5"; $result = mysqli_query($mysqli, $query); if(!$result){echo('no result returned - query wrong'); } </code></pre> <p>but the queries doesnt work and always fires the error. what am i doing wrong here?</p> <p>(my table has columns town, postcode, latitude, longitude)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload