我正在嘗試使用此處提供的php api:https://forums.digitalpoint.com/threads/uk-post-code-distance-calculator-using-php-mysql-3-super-easy-steps.1823109/。代碼中存在一些問題,我不得不解決,但我不知道爲什麼我得到這個錯誤。PHP中未識別的變量
這是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);
非常感謝
卡比爾
包括正確的文件??包括'config.php'上的'config.php'。什麼是目錄結構? – 2014-11-03 11:25:11