In PHP, I have the following code for calculating the distance between two locations:
There are MySQL functions available for doing exactly this.
http://dev.mysql.com/doc/refman/5.0/en/gis-introduction.html