Hey everyone,
I'm having an issue with attempting to select items from mySQL database using a function that grabs the latitude and longitude of a member and shows how far they live from the user logged in. Every time I run my code I get the following error message: FUNCTION databasename.distance does not exist. I have tried everything I can think of, can someone please help me out on this one!
<?php
//// GRAB USER LOGGED-IN SEARCH/PERSONAL INFORMATION.//////
$uBlatlon = '';
$sql_user_data = mysql_query("SELECT id, lat, lon FROM members WHERE id='$id' LIMIT 1") or die (mysql_error());
while($row = mysql_fetch_array($sql_user_data)) {
$userid = $row["id"];
$userlat = $row["lat"];
$userlon = $row["lon"];
}
//// distance FUNCTION ////
function distance($lat1,$lon1,$lat2,$lon2,$unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
$sql_locals_match = mysql_query("SELECT username lat, lon FROM members WHERE distance(lat,lon,'$userlat','$userlon','K') <= '$distance' ORDER BY last_login DESC") or die (mysql_error());
while($row = mysql_fetch_array($sql_locals_match)) {
$memberusername = $row["username"];
$memberlat = $row["lat"];
$memberlon = $row["lon"];
$totaldistance = round(distance($memberlat, $memberlon, $userlat, $userlon,"K"));
if ($totaldistance < $distance) {
$member_display_list .=''.$memberusername.' - '.$totaldistance.' KM AWAY <br><br>';
}else{
continue;
}
}
?>