2014-11-03 50 views
0

我正在尝试使用此处提供的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); 

非常感谢

卡比尔

+0

包括正确的文件??包括'config.php'上的'config.php'。什么是目录结构? – 2014-11-03 11:25:11

回答

1

添加全局关键字在函数太:

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

而且之前打个电话创建连接还:

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

谢谢。这对我来说毫无意义,但它为什么会起作用。我认为直到该函数也有范围的变量,在函数 – kabeersvohra 2014-11-03 12:12:35

1

只需在您的功能中放入global $con即可。

+0

感谢您的答复,它的工作,但我不得不接受以前的答案,因为它更明确,第一个答复,我upvoted你的答案。 – kabeersvohra 2014-11-03 12:11:34