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 9018089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:26:02+00:00 2026-06-16T04:26:02+00:00

Possible Duplicate: how to do location based search Getting similar longitude and latitude from

  • 0

Possible Duplicate:
how to do location based search
Getting similar longitude and latitude from database

I have a dating PHP application working with MySQL database where users enter their location and based on it, other profiles are shown.

Structure of table cities:

int id PK
int country_id FK
varchar(50) name
float longitude
float latitude

Example entry in table:

1 | 1 | New York | 23.20323 | 12.32356

And I want to select all cities which have longitude and latitude less then 23.20323 + 50 km, more then 23.20323 – 50km respectively.

The issue here is not a SQL syntax, but the actual radius calculation.

  • 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-16T04:26:03+00:00Added an answer on June 16, 2026 at 4:26 am

    This should be what you’re after.

    //Get your base city location e.g. New York
    $base_lat = 12.32356;
    $base_lng = 23.20323;
    
    //Get target distance in miles
    $target_distance = 31; //50km is approx 31 miles
    
    //Select all cities that are closer than 50km (31 miles)
    SELECT id, ( 3959 * acos( cos( radians('.$base_lat.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$base_lng.') ) + sin( radians('.$base_lat.') ) * sin( radians( latitude ) ) ) ) AS distance
    FROM cities
    WHERE latitude IS NOT NULL
    AND longitude IS NOT NULL
    HAVING distance < $target_distance
    ORDER BY distance ASC
    
    //Select all cities that are further than 50km (31 miles)
    SELECT id, ( 3959 * acos( cos( radians('.$base_lat.') ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians('.$base_lng.') ) + sin( radians('.$base_lat.') ) * sin( radians( latitude ) ) ) ) AS distance
    FROM cities
    WHERE latitude IS NOT NULL
    AND longitude IS NOT NULL
    HAVING distance > $target_distance
    ORDER BY distance ASC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I get a specific parameter from location.search? I have a
Possible Duplicate: how to determine location based on ip Is there a PHP function
Possible Duplicate: Get xpath location of element in iframe(iframe from my domain) I have
Possible Duplicate: SQL Express Connection string - Relative to application location I have a
Possible Duplicate: Redirect parent window from an iframe action using JavaScript I have a
Possible Duplicate: ZipCode from location I need to find zip code for current location.
Possible Duplicate: get current location in android I'm trying to make an application for
Possible Duplicate: php - Should I call exit() after calling Location: header? I dont
Possible Duplicate: Getting current device language in iOS? I have an app which uses
Possible Duplicate: PHP redirect based on IP AND referrer is it possible to redirect

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.