Math function from php to MySQL

medicaxsas

New Member
I need to use this function form php directly to mysql i'm currently using a simpler formula but this one works better, the porpose is to find the geodetic distance from two point on a map, so basically in my mysql i have a table with cities and avery city has its own lat,long values...his is the formula i would translate\[code\]function distance($lat1, $lng1, $lat2, $lng2){ $pi80 = M_PI / 180; $lat1 *= $pi80; $lng1 *= $pi80; $lat2 *= $pi80; $lng2 *= $pi80; $r = 6372.797; // mean radius of Earth in km $dlat = $lat2 - $lat1; $dlng = $lng2 - $lng1; $a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2); $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); $km = $r * $c; return ($km);}\[/code\]and this is what I'm currently using\[code\]SELECT *, ACOS(SIN($lat1)*SIN(cities_tb.lat) + COS($lat1)*COS(cities_tb.lat)*COS(cities_tb.lon-$lon1))*6371 as distanceFROM cities_tb ...etc...\[/code\]where \[code\]$lat1\[/code\] and \[code\]$lon1\[/code\] are values coming from the PHP page
 
Back
Top