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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:48:16+00:00 2026-06-17T02:48:16+00:00

I have a working PHP function that takes in latitude/longitude coordinates and a distance

  • 0

I have a working PHP function that takes in latitude/longitude coordinates and a distance in miles, then tests row by row in a MySQL table of zip codes/lat-lon pairs (40,000+ rows) whether each zip code/lat-lon row falls within the specified distance or not.

I know that this method is terribly inefficient and I would like to do all of the trigonometry and distance calculation in the SQL query rather than with PHP. Can anyone help me out here? I feel like I’ve tried everything but I just can’t get the SQL query correct. Any help would be greatly appreciated!

Existing PHP function:

function getZips($lat, $lon, $radius)
{   

//MySQL bit
$zipQuery = mysql_query("SELECT * FROM zip_codes");
while ($row = mysql_fetch_assoc($zipQuery))
{

    $lat2 = $row['latitude'];
    $lon2 = $row['longitude'];

    $distance = (3958*3.1415926*sqrt(($lat2-$lat)*($lat2-$lat) + cos($lat2/57.29578)*cos($lat/57.29578)*($lon2-$lon)*($lon2-$lon))/180);

    if ($distance <= $radius)
    {
        $zip = $row['zip'];
        echo "Zip in range: <b>" .$zip . "</b><br>";
    }

}

}
  • 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-17T02:48:17+00:00Added an answer on June 17, 2026 at 2:48 am
    SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM logic_zipcodes HAVING distance <= {$radius} ORDER BY distance
    

    can’t remember where I got this originaly, but I have been using it for a long time. Here is my function:

    function zipsearch($start, $rad) {
    // ITITIAL POINT
    $result = mysql_query("SELECT latitude,longitude FROM logic_zipcodes WHERE zipcode='" . $start . "'");
    while($row = mysql_fetch_array($result)) {
        $coords = array('latitude' => $row['latitude'], 'longitude' => $row['longitude']);
    }
    //RADIUS
    $radius = $rad;
    // SQL FOR MILES
    $sql = "SELECT zipcode, ( 3959 * acos( cos( radians( {$coords['latitude']} ) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians( {$coords['longitude']} ) ) + sin( radians( {$coords['latitude']} ) ) * sin( radians( latitude ) ) ) ) AS distance FROM logic_zipcodes HAVING distance <= {$radius} ORDER BY distance";
    
    // OUTPUT THE ZIPCODES AND DISTANCES
    $query = mysql_query($sql);
    $zips = array();
    while($row = mysql_fetch_assoc($query)) {
        $zips[] = $row['zipcode'];
    }
    return $zips;
    }
     function getZips($zip, $rad) {
    $zipcode = qt($zip);
    $andstatement = implode("','", zipsearch($zipcode, $rad));
    return "AND zipcode in('" . $andstatement . "')";
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on an AJAX website (HTML,CSS,JavaScript, AJAX, PHP, MySQL). I have multiple javascript functions
I have working website in PHP with a MySQL backend which has several tables
I have been working on on an AJAX chat application using php, mysql. It's
I am working with php and want to have a page that has a
i used to have a function defined like this (that is working fine under
I am writing a PHP/MYSQL search function which takes a users search query (as
I am working on old sites and updating the deprecated php functions. I have
I have my php app working fine, performing some other Graph API functions. However,
We currently have a working php mail script, this works fine and as we
at my working place (php only) we have a base class for database abstraction.

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.