我有以下脚本,它基本上应该从我的 MySQL 表中获取字段,其中包括经度和纬度,并计算驾驶距离,然后将其作为列表回显给用户。
由于某种原因,脚本仅打印第一个结果,然后抛出错误 fatal error :无法重新声明 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();
任何关于如何解决这个问题的提示都将受到高度赞赏。
最佳答案
试试这个:
不能在循环中声明函数
// 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();
关于php - 为什么我的脚本不断重新声明 php 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37497254/