Monday 15 February 2010

mysql - Variable not recognised in PHP -



mysql - Variable not recognised in PHP -

i trying utilize php api provided here: https://forums.digitalpoint.com/threads/uk-post-code-distance-calculator-using-php-mysql-3-super-easy-steps.1823109/. there few problems in code had prepare dont know why getting error.

this config.php file:

<?php include('/config.php'); /* * created on may 20, 2010 * * programmed manoj kumar */ //connect database $host_sql = $host; $username_sql = $username; $password_sql = $password; $dbname_sql = $dbname; $con = mysqli_connect($host_sql, $username_sql, $password_sql, $dbname_sql); ?>

and distcalc.php file

<?php /* * created on jun 1, 2010 * * alter template generated file go * window - preferences - phpeclipse - php - code templates */ include('/api/postcodedistance/config.php'); //distcalc("nw4 4te","ze3 8hb"); function distcalc($pc1_full,$pc2_full) { #convert post code upper case , trim variable $pc1_full = strtoupper(trim($pc1_full)); #remove spaces $pc1_full = str_replace(" ","",$pc1_full); #trim lastly 3 characters off end $pc1 = substr($pc1_full,0,strlen($pc1_full)-3); #convert post code upper case , trim variable $pc2_full = strtoupper(trim($pc2_full)); #remove spaces $pc2_full = str_replace(" ","",$pc2_full); #trim lastly 3 characters off end $pc2 = substr($pc2_full,0,strlen($pc2_full)-3); $sql="select * `hwz_postcodes` `outcode` = '$pc1'"; $result=mysqli_query($con, $sql); $row=mysqli_fetch_array($result); $pc1_lat=$row['latitude']; $pc1_long=$row['longitude']; $sql="select * `hwz_postcodes` `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; homecoming $distance; } function getdistance($lat1, $long1, $lat2, $long2){ #$earth = 6371; #km alter 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); homecoming $d; } ?>

this error getting:

notice: undefined variable: con in c:\users\kabeer\dropbox\projects\project\api\postcodedistance\distcalc.php on line 38

con defined confused problem is. if create con global parsing error. this:

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

many thanks

kabeer

add global keyword in function too:

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

and befor create connection also:

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

php mysql undefined

No comments:

Post a Comment