0

I have a SELECT SQL statement that returns a Latitude, Longitude and Distance as result (using the Haversine formula). Now I need to store these values as variables within the PHP script as I need to do further processing on the results. Can anyone tell me how I can do this?

Here is my SQL Statement

UPDATED

$query = $db->prepare("SELECT `Latitude`, `Longitude`, (6378160 * acos( cos( radians(:latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(:longitude) ) + sin( radians(:latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM `mytable` HAVING distance < :radius ORDER BY distance;");
// Assign parameters
$query->bindParam(':latitude',$newLatitude);
$query->bindParam(':longitude',$newLongitude);
$query->bindParam(':radius',$radius);

//Execute query
$query->execute();
$q   = $db->query($query);
$q->setFetchMode(PDO::FETCH_ASSOC);

// fetch
while($r = $q->fetch()){
  $eLatitude = $r->Latitude;
  $eLongitude = $r>Longitude;
  $distance = $r->Distance;
}

I updated my php code above. Is this code's syntax correct? Or the way I'm doing it is all wrong?

6
  • execute query and in while loop fetch results and assign them to variables, what's the problem Commented Jan 3, 2013 at 14:44
  • Also, there are much more efficient ways to do this if you search here for mysql geolocation. Commented Jan 3, 2013 at 14:46
  • 1
    @vodich I am new to PDO/PHP altogether so I am not pretty sure how this can be done Commented Jan 3, 2013 at 14:46
  • phpeveryday.com/articles/PDO-Posibble-Fetch-Mode-P548.html Commented Jan 3, 2013 at 14:50
  • php.net/manual/en/pdostatement.fetch.php ? Commented Jan 3, 2013 at 14:51

2 Answers 2

1

PDO::FETCH_ASSOC: returns an array indexed by column name Manual

while($r = $q->fetch()){
  echo $r['Latitude']. "\n"; //Or do what ever instead of echo
  echo $r['Longitude']. "\n";
  echo $r['Distance']. "\n";
}

ie XML
while($r = $q->fetch()){
  $node = $dom->createElement("marker");
  $newnode->setAttribute("lat",  $r['Latitude']);
  $newnode->setAttribute("lng",  $r['Longitude']);
  $newnode->setAttribute("distance",  $r['Distance']);
}

ie JSON
while($r = $q->fetch()){
  data[] = $r;
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can use

$queryresult = mysql_query("SELECT `Latitude`, `Longitude`, (6378160 * acos( cos( radians(:latitude) ) * cos( radians( Latitude ) ) * cos( radians( Longitude ) - radians(:longitude) ) + sin( radians(:latitude) ) * sin( radians( Latitude ) ) ) ) AS distance FROM `mytable` HAVING distance < :radius ORDER BY distance;");
if (!$queryresult) {
    echo 'Error ' . mysql_error();
    exit;
}
$resultrow = mysql_fetch_row($queryresult);

echo $resultrow[0];  // this will be latitude
echo $resultrow[1];  // this will be longitude

1 Comment

Since the original mysql functions have now been deprecated and will be removed in the future, please don't provide examples using the mysql functions, especially when the person asking the question is already using MySQLi.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.