gpt4 book ai didi

php - PHP 无法识别变量

转载 作者:行者123 更新时间:2023-11-29 07:50:07 26 4
gpt4 key购买 nike

我正在尝试使用此处提供的 php api:https://forums.digitalpoint.com/threads/uk-post-code-distance-calculator-using-php-mysql-3-super-easy-steps.1823109/ 。代码中有一些问题我必须修复,但我不知道为什么会收到此错误。

这是 config.php 文件:

        <?php
include('/config.php');
/*
* Created on May 20, 2010
*
* Programmed by Manoj Kumar
*/

//Connect To Database
$host_sql = $host;
$username_sql = $username;
$password_sql = $password;
$dbname_sql = $dbname;


$con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);

?>

这是 distCalc.php 文件

        <?php
/*
* Created on Jun 1, 2010
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/

include('/api/postcodedistance/config.php');


//distCalc("NW4 4TE","ZE3 8HB");

function distCalc($pc1_full,$pc2_full) {


#Convert the post code to upper case and trim the variable
$pc1_full = strtoupper(trim($pc1_full));

#Remove any spaces
$pc1_full = str_replace(" ","",$pc1_full);

#Trim the last 3 characters off the end
$pc1 = substr($pc1_full,0,strlen($pc1_full)-3);

#Convert the post code to upper case and trim the variable
$pc2_full = strtoupper(trim($pc2_full));

#Remove any spaces
$pc2_full = str_replace(" ","",$pc2_full);

#Trim the last 3 characters off the end
$pc2 = substr($pc2_full,0,strlen($pc2_full)-3);



$sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc1'";
$result=mysqli_query($con, $sql);
$row=mysqli_fetch_array($result);

$pc1_lat=$row['latitude'];
$pc1_long=$row['longitude'];


$sql="SELECT * FROM `hwz_postcodes` WHERE `outcode` = '$pc2'";
$result=mysqli_query($con, $sql);
$row=mysqli_fetch_array($result);

$pc2_lat=$row['latitude'];
$pc2_long=$row['longitude'];

//echo "<br/>".$pc1."<br/> ".$pc1_lat."<br/> ".$pc1_long;
//echo "<br/>".$pc2."<br/> ".$pc2_lat."<br/> ".$pc2_long;

$distance = getDistance($pc1_lat, $pc1_long, $pc2_lat, $pc2_long);

//echo "<br/>Distance:".$distance;

return $distance;

}


function getDistance($lat1, $long1, $lat2, $long2){

#$earth = 6371; #km change accordingly
$earth = 3960; #miles

#Point 1 cords
$lat1 = deg2rad($lat1);
$long1= deg2rad($long1);

#Point 2 cords
$lat2 = deg2rad($lat2);
$long2= deg2rad($long2);

#Haversine Formula
$dlong=$long2-$long1;
$dlat=$lat2-$lat1;

$sinlat=sin($dlat/2);
$sinlong=sin($dlong/2);

$a=($sinlat*$sinlat)+cos($lat1)*cos($lat2)*($sinlong*$sinlong);

$c=2*asin(min(1,sqrt($a)));

$d=round($earth*$c);

return $d;

}


?>

这是我收到的错误:

Notice: Undefined variable: con in C:\Users\Kabeer\Dropbox\Projects\project\api\postcodedistance\distCalc.php on line 38

Con 已定义,所以我很困惑问题是什么。如果我将 con 设置为全局,则会出现解析错误。我这样做是这样的:

global $con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);

非常感谢

卡比尔

最佳答案

也在您的函数中添加全局关键字:

function distCalc($pc1_full,$pc2_full) {
global $con;
//...
}

在您创建该连接之前:

global $con;
$con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql);

关于php - PHP 无法识别变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26713035/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com