I have a database with companies and their zip code addresses. I created a function that would give me names of companies that is within a certain radius. However, I am very bad at MYSQL and the query part is not working. I researched the function from this site and is suppose to work if carried out correctly.I keep getting an error saying that results2 is false. Can someone help me figure out what is wrong about my syntax following $sql =?
function zipcodeRadius($lat, $lon, $radius)
{
$radius = $radius ? $radius : 20;
$lat = intval($lat);
$lon = intval($lon);
$radius = intval($radius);
$sql = "SELECT company,( 3959 * ACOS(COS(RADIANS($lat) ) * COS(RADIANS( lat ) ) * COS( RADIANS( longitude ) - RADIANS($lon) ) + SIN(RADIANS($lat) ) * SIN(RADIANS( lat ) ) ) ) AS distance FROM COMPANY WHERE distance < 50 ORDER BY distance";
$result2 = mysql_query($sql);
while($row2= mysql_fetch_array($result2))
{
echo $row2[company]. " - ". $row2[zip];
echo "<br/>";
}
}