0

Possible Duplicate:
Fastest Way to Find Distance Between Two Lat/Long Points

I have a database that includes the town name and the longitude and latitude coordinate numbers. I have a system where a user enters the pick up location and destination when they want to hire a bus. I am able to query mysql database to bring out the two locations from the hire being made but am not able to get the coordinates of the two locations that are stored in another table on the database. I'm pretty sure the code to work out the distance between the two locations works as it worked with just putting numbers before rather than variables but I cannot figure out what I'm doing wrong. Any help is appreciated, thanks.

$result = mysql_query("SELECT hireid FROM hire ORDER BY customerid DESC LIMIT 1");

while ($row = mysql_fetch_assoc($result)){

$hireid = $row['hireid'];
}
echo "hireid: " . $hireid . "<br/>";

$result = mysql_query("SELECT pickuplocation, destination FROM hire WHERE     hireid='$hireid'");

while ($row = mysql_fetch_assoc($result)){

$pickuplocation = $row['pickuplocation'];
$destination = $row['destination'];

}

echo "pickuplocation: " . $pickuplocation . "<br/>";
echo "destination: " . $destination . "<br/>";


$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE  town='$pickuplocation'") or die ('Error: '.mysql_error ());

while ($row = mysql_fetch_array($result)){

$long1 = $row['longitude'];
$lat1 = $row['latitude'];

}
"<br/>";
echo "Location1 longitude: " . $long1 . "<br/>";
echo "Location1 latitude: " . $lat1 . "<br/>";


$result = mysql_query("SELECT longitude, latitude FROM coordinates WHERE     town='$destination'") or die ('Error: '.mysql_error ());

while ($row = mysql_fetch_array($result)){

$long2 = $row['longitude'];
$lat2 = $row['latitude'];

}
"<br/>";

echo "Location2 longitude: " . $long2 . "<br/>";
echo "Location2 latitude: " . $lat2 . "<br/>";


function getDistanceBetweenPointsNew($lat1, $long1, $lat2, $long2) {
$theta = $long1 - $long2;
$miles = (sin(deg2rad($lat1)) * sin(deg2rad($lat2))) + (cos(deg2rad($lat1)) *   cos(deg2rad($lat2)) * cos(deg2rad($theta)));
$miles = acos($miles);
$miles = rad2deg($miles);
$miles = $miles * 60 * 1.1515;
$feet = $miles * 5280;
$yards = $feet / 3;
$kilometers = $miles * 1.609344;
$meters = $kilometers * 1000;
return compact('miles') . "<br/>";
}


$point1 = array('lat' => $lat1, 'long' => $long1);
$point2 = array('lat' => $lat2, 'long' => $long2);
$distance = getDistanceBetweenPointsNew($point1['lat'], $point1['long'],     $point2['lat'], $point2['long']);
foreach ($distance as $unit => $value) {
echo $unit.': '.number_format($value,4).'<br />';
}
Community
  • 1
  • 1
Lubblobba
  • 65
  • 4
  • 14
  • 2
    The `mysql_*` functions will be [deprecated in PHP 5.5](http://php.net/manual/en/faq.databases.php#faq.databases.mysql.deprecated). It is not recommended for writing new code as it will be removed in the future. Instead, either the [MySQLi](http://php.net/manual/en/book.mysqli.php) or [PDO](http://php.net/manual/en/book.pdo.php) and [be a better PHP Developer](http://jason.pureconcepts.net/2012/08/better-php-developer/). – Jason McCreary Jan 16 '13 at 18:12
  • 1
    "I am not able"... What error and where? :) – gd1 Jan 16 '13 at 18:13
  • ah well i'm new to php and my teachers decided to teach me the old ways of php and i have a limited time with my project so i'm not sure about learning newer codes. well basically latitude and longitude for the pickuplocation and destination are not being taken from the database and displayed but i can't see why not – Lubblobba Jan 16 '13 at 18:15
  • i've updated my code, i've fixed the issue i had and can now retrieve long and lat coordinates for two locations but my code to find distance has stopped working and i get the error "Warning: Invalid argument supplied for foreach() in C:\Users\Laura\Documents\COMPY\server\www\thirdform.php on line 108". could it be the $lat1 $long1 $lat2 $long2 variables because it works when these are just numbers. – Lubblobba Jan 16 '13 at 18:35
  • There are maybe even better ways to store and handle geographic data, e.g. http://en.wikipedia.org/wiki/PostGIS – feeela Jan 16 '13 at 18:36
  • Always be very careful with [SQL escaping issues](http://bobby-tables.com/php). Learning with `mysql_query` is like learning using [punch-cards](http://en.wikipedia.org/wiki/Punched_card), it's a technique that's woefully obsolete. Sadly, anyone who teaches these methods is doing a dis-service to their students. – tadman Jan 16 '13 at 18:37
  • Do all calculations in SQL. Create procedure/function – nkamm Jan 16 '13 at 18:39

0 Answers0