Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9267563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:34:51+00:00 2026-06-18T14:34:51+00:00

using this query from google: link Im trying to create a location search, returning

  • 0

using this query from google: link

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.

to start, i find the lat + long of the inputted location:

    $location = 'farnborough';

    function getlatlang($location) {
    $geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address='. urlencode($location) .'&sensor=false');  
    $output= json_decode($geocode);  
    return $output->results[0]->geometry->location; 
    }

    $objlocation = getlatlang($location);  
    $latitude = $objlocation->lat;  
    $longitude = $objlocation->lng;

this works fine, i can echo the result and the values are correct.

then using the query in the above link, i try:

    $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 < 25 ORDER BY distance ASC LIMIT 0 , 5";
    $result = mysqli_query($mysqli, $query);
    if(!$result){echo('no result returned - query wrong');
    }

but the queries doesnt work and always fires the error. what am i doing wrong here?

(my table has columns town, postcode, latitude, longitude)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:34:52+00:00Added an answer on June 18, 2026 at 2:34 pm

    I’m making the assumption here, that you have changed the column names from lat, lng to latitude, longitude in your table definition.

    And under the lack of the actual error message, this is the best guess:

    SELECT
      town,
      (
        3959 * acos(
          cos(
            radians($latitude)
          ) * cos(
            radians(latitude)
          ) * cos(
            radians(longitude) - radians($longitude)
          ) + sin(
            radians($latitude)
          ) * sin(
            radians(latitude)
          )
        )
      ) AS distance
    FROM
      uk_postcode_05
    HAVING
      distance < 25
    ORDER BY
      distance ASC
    LIMIT
      0, 5
    

    this should fix the issue, since in your query lat and lng where undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to return voice messages from a database using this query :
I am trying to search within Google Cache, so I need to fire this
When using this SQL query: Select * from table_name what happens if the name
I am using this query: SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN
I'm using the JavaScript mongodb driver from nodejs. I want to do this query
i've been using the following query: select LEN(columnname) as columnmame from dbo.amu_datastaging This works,
I'm using this SQL query to create an index: $query = CREATE INDEX id_index2
I want DBSession.query(Article).group_by(Article.created.month).all() But this query can't using How do I do this using
Right now I am using this code: string url = http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=hey&esrch=FT1; string source =
The following code taken from: Java code for using google custom search API .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.