loops - PHP - Looping $_POST in PHP Query -
first im student. im trying make looping in $_post php statements this
if ($_post) { //get value of latitude, longitude, radius site.php calculate in query above include('config/dbconnect.php'); //database connection $lat = array(); $lon = array(); $rad = array(); $stmt = array(); $sql = array(); $lat1 = $_post['lat1']; //get value of latitude 1 $lon1 = $_post['lon1']; //get value of longitude 1 $rad1 = $_post['rad1']; //get value of radius 1 $endrow = $_post['endrow']; ($i = 2; $i <= $endrow; $i++) { $lat[$i] = $_post['lat'.$i]; //get value of latitude n $lon[$i] = $_post['lon'.$i]; //get value of longitude n $rad[$i] = $_post['rad'.$i]; //get value of radius n } //haversine formula , view match site ci , d2g_trx ci $sql = "select site.*,d2g_trx.*, ( 6371 * acos( cos( radians($lat1) ) * cos( radians( site.latitude ) ) * cos( radians( site.longitude ) - radians($lon1) ) + sin( radians($lat1) ) * sin( radians( site.latitude ) ) ) ) distance site inner join d2g_trx on site.ci = d2g_trx.ci having distance <=$rad1 order distance limit 0 , 50"; $stmt = $dbh->query($sql)->fetchall(); // input 1 ($j = 2; $j <= $endrow; $j++) { //haversine formula , view match site ci , d2g_trx ci $sql[$j] = "select site.*,d2g_trx.*, ( 6371 * acos( cos( radians($lat[$j]) ) * cos( radians( site.latitude ) ) * cos( radians( site.longitude ) - radians($lon[$j]) ) + sin( radians($lat[$j]) ) * sin( radians( site.latitude ) ) ) ) distance site inner join d2g_trx on site.ci = d2g_trx.ci having distance <=$rad[$j] order distance limit 0 , 50"; $stmt[$j] = $dbh->query($sql[$j])->fetchall(); //input n } }
i got error
fatal error: call member function fetchall() on non-object in f:\xampp\htdocs\kp\getsite.php on line 28
anyway, line 28 one
$stmt[$j]= $dbh->query($sql[$j])->fetchall(); //input n
sorry, english bad :(
any idea how fix sir ?
thanks before
just because can this:
$dbh->query($sql[$j])->fetchall();
doesn't mean should. @ least testing purposes should @ return values (i assume use pdo):
$result = $db->query($sql[$j]); if($result === false) { print_r($db->errorinfo()); exit; } $stmt[$j] = $result->fetch();
Comments
Post a Comment