I want to return the distance and the latitude of the closest store in my app. But when I run this script in my browser I get null. If I remove the ['latitude'], then I get array(1) { [0]=> bool(true) }. This is the way I call the function. Do I have to have a table to loop the result of the function and feed the rows?
$damn = getClosestS(37.292999, -122.555333);
var_dump($damn);
function getClosestDriver($plat, $plon) {
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT id, latitude, longitude, ( 3959 * acos( cos( radians($plat) ) *
cos( radians( latitude ) ) * cos( radians( longitude ) - radians($plon) ) +
sin( radians($plat) ) * sin( radians( latitude ) ) ) ) AS distance
FROM drivers HAVING distance < 2500 ORDER BY distance LIMIT 0 , 20");
$stmt->execute();
// set the resulting array to associative
$result = array($stmt->setFetchMode(PDO::FETCH_ASSOC));
return $result['latitude'];
} catch (PDOException $e) {
echo $stmt . "<br>" . $e->getMessage();
}
$conn = null;
}