首先是我的学生。我只是试图在这样的$ _POST php语句中进行循环
if ($_POST) { //Get value of Latitude, Longitude, Radius from site.php to 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'];
for ($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 and view only match Site CI and 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 ) ) ) ) AS distance
FROM site
INNER JOIN d2g_trx ON site.ci = d2g_trx.CI
HAVING distance <=$rad1
ORDER BY distance
LIMIT 0 , 50";
$stmt = $dbh->query($sql)->fetchAll(); // Input 1
for ($j = 2; $j <= $endrow; $j++) {
//Haversine Formula and view only match Site CI and 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 ) ) ) ) AS distance
FROM site
INNER JOIN d2g_trx ON site.ci = d2g_trx.CI
HAVING distance <=$rad[$j]
ORDER BY distance
LIMIT 0 , 50";
$stmt[$j] = $dbh->query($sql[$j])->fetchAll(); //Input n
}
}
我得到了这样的错误
致命错误:在非对象中调用成员函数fetchAll() 第28行的F:\ XAMPP \ htdocs \ kp \ getsite.php
无论如何,第28行就是这个
$stmt[$j]= $dbh->query($sql[$j])->fetchAll(); //Input n
对不起,我的英语非常糟糕:(
知道怎么解决这个先生吗?
之前谢谢
答案 0 :(得分:0)
仅仅因为你可以这样做:
$dbh->query($sql[$j])->fetchAll();
并不代表你应该这样做。至少出于测试目的,您应该真正查看返回值(我假设您使用PDO):
$result = $db->query($sql[$j]);
if($result === false) {
print_r($db->errorInfo());
exit;
}
$stmt[$j] = $result->fetch();
答案 1 :(得分:0)
最后,它可能就像这样
if( $_POST ){ //Get value of Latitude, Longitude, Radius from site.php to calculate in Query above
include('config/dbconnect.php'); //Database connection
$tempsss = array("");
$lat = array("");
$lon = array("");
$rad = array("");
$j=1;
foreach($_POST as $k => $v) {
if(strpos($k, 'lat') === 0) {
$lat[$j] =$v; //Get value of Latitude
$j++;
}
};
$z=1;
foreach($_POST as $k => $v) {
if(strpos($k, 'lon') === 0) {
$lon[$z] =$v; //Get value of Latitude
$z++;
}
};
$h=1;
foreach($_POST as $k => $v) {
if(strpos($k, 'rad') === 0) {
$rad[$h] =$v; //Get value of Latitude
$h++;
}
};