MySQL Function to Determine Zip Code Proximity / Range

John

New Member
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:\[code\]id,usernameinfolatitudelongituderange\[/code\]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\[code\]$userLat\[/code\] = The latitude of the user that is running the search\[code\]$userLong\[/code\] = The longitude of the user that is running the search\[code\]SELECT info FROM table WHERE sqrt(($userLat - lat)^2 - ($userLong - long)^2) <= range ORDER BY username ASC\[/code\]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 + SOLUTIONI 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:http://stackoverflow.com/questions/3640448/mysql-user-defined-function-for-latitude-longitude-syntax
 
Back
Top