alexcolson.com
|
[Printable Version] |
| Using ZIP Codes in Web Applications Home > Tech Talk > Article Modified: Saturday, March 8, 2008 6:09:29 PM by Alex Colson Tags: MySQL, SQL, Web Development, ZIP Codes Using ZIP Codes in a web application allows users to do many things, including:
Assemble the database as follows: zip, char(5), ex: 10108Once you have a database full of ZIP Codes and other data, we can start working with SQL queries to find cities, locations, distances, and more. REMEMBER: When allowing users to input data, such as a City or ZIP Code, you should use MySQL Prep to prevent SQL Injection. Finding the first ZIP Code based on a City and State being entered: select zip from zipcodes where city like '%$city%' and state = '$state' limit 1;Where $city is input as a city name and $state is input as state initials. Finding Longitude and Latitude of a ZIP Code: select longitude, latitude from zipcodes where zip = '$zip';Where $zip is input as a ZIP Code. Finding cities within a range of a Longitude and Latitude: select city, state, (((acos(sin(($lat*pi()/180)) * sin((latitude*pi()/180)) + cos(($lat*pi()/180)) * cos((latitude*pi()/180)) * cos((($lng - longitude)*pi()/180))))*180/pi())*60*1.1515) as distance from zipcodes where distance <= $range order by distance;Where $lat is a specific latitude, $lng is a specific longitude, and $range is a range in miles that you want to search. Finding distance to a ZIP Codes: select (((acos(sin(($lat*pi()/180)) * sin((latitude*pi()/180)) + cos(($lat*pi()/180)) * cos((latitude*pi()/180)) * cos((($lng - longitude)*pi()/180))))*180/pi())*60*1.1515) as distance from zipcodes where zip = '$zip';Where $lat is the originating latitude, $lng is the originating longitude, and $zip is the destination ZIP Code. |
||
| Sunday, September 5, 2010 10:21:03 AM EST (0.0146 s) | Copyright © 2008 Alex Colson |