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

  • SEARCH
  • Home
  • 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 1014661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:17:00+00:00 2026-05-16T10:17:00+00:00

I have been able to create php function to determine a list of zip

  • 0

I have been able to create php function to determine a list of zip codes within a certain range of a given zip code. However, my next task is a bit more complex.

Lets say the table has the following columns:

id,
username
info
latitude
longitude
range

Each record has a unique row id, some information, a latitude, a longitude, and a maximum range from those coordinates that the person wants this entry to be found for. So, if I create an entry with the coordinates -20, 50 with a range of 15, that means I only want people within 15 miles of the coordinates -20, 50 to be able to see my entry. Figuring out the latitude and longitude of the user running the search is a trivial matter.

When a user is searching through the database, all records should be retrieved for latitude and longitude coordinates within the value of range from the users lat/long.

So, a non-functional example of code using the distance formula to illustrate this would be

$userLat = The latitude of the user that is running the search

$userLong = The longitude of the user that is running the search

SELECT info FROM table 
WHERE sqrt(($userLat - lat)^2 - ($userLong - long)^2) <= range 
ORDER BY username ASC

That would be ideal, but it is not valid from a coding standpoint. Is there any way to run the above comparisons using PHP and MySQL in one query? This would involve being able to run operations using multiple column values from a given row.


UPDATE + SOLUTION

I created another question entry that addresses this issue and has a very compact function to do what this question wanted. The function given by coderpros served as the inspiration for it (his function has a lot better handling for certain situations). However, since I am using my function with a great degree of control over the input, I created a separate function. It can be found at the link below:

MySQL User Defined Function for Latitude Longitude Syntax

  • 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-05-16T10:17:00+00:00Added an answer on May 16, 2026 at 10:17 am

    This nifty lil mySQL function that I wrote should definitely do the trick for you.

    BEGIN
       DECLARE x decimal(18,15);
       DECLARE EarthRadius decimal(7,3);
       DECLARE distInSelectedUnit decimal(18, 10);
    
       IF originLatitude = relativeLatitude AND originLongitude = relativeLongitude THEN
              RETURN 0; -- same lat/lon points, 0 distance
       END IF;
    
       -- default unit of measurement will be miles
       IF measure != 'Miles' AND measure != 'Kilometers' THEN
              SET measure = 'Miles';
       END IF;
    
       -- lat and lon values must be within -180 and 180.
       IF originLatitude < -180 OR relativeLatitude < -180 OR originLongitude < -180 OR relativeLongitude < -180 THEN
              RETURN 0;
       END IF;
    
       IF originLatitude > 180 OR relativeLatitude > 180 OR originLongitude > 180 OR relativeLongitude > 180 THEN
             RETURN 0;
       END IF;
    
       SET x = 0.0;
    
       -- convert from degrees to radians
       SET originLatitude = originLatitude * PI() / 180.0,
           originLongitude = originLongitude * PI() / 180.0,
           relativeLatitude = relativeLatitude * PI() / 180.0,
           relativeLongitude = relativeLongitude * PI() / 180.0;
    
       -- distance formula, accurate to within 30 feet
       SET x = Sin(originLatitude) * Sin(relativeLatitude) + Cos(originLatitude) * Cos(relativeLatitude) * Cos(relativeLongitude - originLongitude);
    
       IF 1 = x THEN
              SET distInSelectedUnit = 0; -- same lat/long points
              -- not enough precision in MySQL to detect this earlier in the function
       END IF;
    
       SET EarthRadius = 3963.189;
       SET distInSelectedUnit = EarthRadius * (-1 * Atan(x / Sqrt(1 - x * x)) + PI() / 2);
    
       -- convert the result to kilometers if desired
       IF measure = 'Kilometers' THEN
              SET distInSelectedUnit = MilesToKilometers(distInSelectedUnit);
       END IF;
    
       RETURN distInSelectedUnit;
    END
    

    It takes originLatitude, originLongitude, relativeLatitude, relativeLongitude, and measure as parameters. Measure can simply be Miles or Kilometers

    Hope that helps!

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

Sidebar

Related Questions

I am a beginner with MSBuild. So far I have been able to create
I have been able to find lots of examples of adding a Dropdown list
I have successfully created a custom Facebook open graph action, and have been able
I have been able to run my C# .net 3.5 app just fine on
I have been able to make my photos large for single photo posts on
I have been able to design my layout so that it has 2 columns
I have been able to set up OBDC in Filemaker, and added table ,
I have been trying different things and still have been able to get exactly
Hi I have been able to extract a VARCHAR to a date using string_to_date
What I have been able to grasp from reading the source and documentation from

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.