nick nick - 6 months ago 24
PHP Question

Why does my script keep redeclaring php function?

I have the following script that should basically fetch fields from my MySQL tables which include longitude and latitude and the calculate the driving distance which is then echoed to the user as a list.

For some reason the script only prints the first result from the and then throws an error

Fatal Error: Cannot redeclare GetDrivingDistance()


mysqli_close($conn);
$servername = "localhost";
$username = "user";
$password = "pwd";
$dbname = "buses";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT *
FROM matatus
JOIN registered_matatus
ON matatus.user_id=registered_matatus.user_id
ORDER BY matatus.vehicle_id
ASC LIMIT 4";
if ($result->num_rows > 0) {
// output data of each row
echo "CON Select a matatu\n";
while($row = $result->fetch_assoc()) {
//Start of calculate distance and time
$lat1 = "-1.283180";
$long1 = "36.822462";
$lat2 = $row["lat"];
$long2 = $row["lng"];

function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
$url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$response = curl_exec($ch);
curl_close($ch);
$response_a = json_decode($response, true);
$dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
$time = $response_a['rows'][0]['elements'][0]['duration']['text'];
return array('distance' => $dist, 'time' => $time);
}
$dist = GetDrivingDistance($lat1, $lat2, $long1, $long2);
$distance = $dist['distance'];
$time = $dist['time'];
//End of calculate distance and time
echo "". $row["id"]. " ". $row["reg_number"]. " (" . $row["sacco"] . ") -$time\n";
}
} else {
echo "0 results";
}
$conn->close();


Any heads up on how to go around this could be highly appreciated.

Answer

try this :
You cannot declare function in loop

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
} 
$sql = "SELECT * 
                    FROM matatus 
                    JOIN registered_matatus
                    ON matatus.user_id=registered_matatus.user_id 
                    ORDER BY matatus.vehicle_id 
                    ASC LIMIT 4";
if ($result->num_rows > 0) {
     // output data of each row
     echo "CON Select a matatu\n";

function GetDrivingDistance($lat1, $lat2, $long1, $long2)
{
    $url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=".$lat1.",".$long1."&destinations=".$lat2.",".$long2."&mode=driving&language=pl-PL";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_PROXYPORT, 3128);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);
    $response_a = json_decode($response, true);
    $dist = $response_a['rows'][0]['elements'][0]['distance']['text'];
    $time = $response_a['rows'][0]['elements'][0]['duration']['text'];
    return array('distance' => $dist, 'time' => $time);
}
while($row = $result->fetch_assoc()) {
//Start of  calculate distance and time
$lat1 = "-1.283180";
$long1 = "36.822462";
$lat2 = $row["lat"];
$long2 = $row["lng"];
$dist = GetDrivingDistance($lat1, $lat2, $long1, $long2);
$distance = $dist['distance'];
$time = $dist['time'];
//End of calculate distance and time
         echo "". $row["id"]. " ". $row["reg_number"]. " (" . $row["sacco"] . ") -$time\n";
     }
} else {
     echo "0 results";
}
$conn->close();