我有以下脚本,基本上应该从我的MySQL表中获取包含经度和纬度的字段,并计算行驶距离,然后以列表的形式回显给用户。
由于某种原因,脚本只打印出第一个结果,然后抛出错误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();
任何关于如何解决这个问题的人都会非常感激。
答案 0 :(得分:1)
试试这个:
你不能在循环中声明函数
// 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();
答案 1 :(得分:0)
你的GetDrivingDistance函数在循环中。你不能在php中一次又一次地声明具有相同名称的函数。它会引导你得到同样的错误。
试试这个
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"];
$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";
}
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);
}
$conn->close();