我想找到保存在数据库表中的最近位置
nama_sekolah latitude(lat) longitude(lon)
SMA 1 -6.811587 110.851419
SMA 2 -6.804751 110.825048
SMA 3 -6.804658 110.874171
SMA 4 -6.787235 110.865934
SMA 5 -6.743791 110.839555
SMA 6 -6.833336 110.869366
SMA 7 -6.805317 110.926164
我试过下面的代码,但没有一个结果nearestCat是复选框,$terdekat是千米中距离的输入文本
<?php
if (isset($_POST['nearestCat'])) {
$origLat = -6.811;
$origLon = 110.851;
$dist = $terdekat; // This is the maximum distance (in km) away from $origLat, $origLon in which to search
$query = "SELECT*, ( 6371 * acos( cos( radians($origLat) ) * cos( radians( lat ) ) * cos( radians( lon ) - radians($origLon) ) + sin( radians($origLat) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < $dist ORDER BY distance LIMIT 0 , 20";
$no=1;
while ($hasil = mysql_query($query)) { ?>
<table border: 1px solid black; >
<tr text-align:center>
<th>No</th><th>NSS</th><th>Nama Sekolah</th><th>Jenis Sekolah</th><th>Akreditasi</th><th>Kecamatan</th><th>Alamat</th><th colspan="2">Aksi</th>
</tr>
<tr text-align='center'>
<td align='center'><? echo "$no" ?></td>
<td><? echo "$data[nss]"?></td>
<td><? echo "$data[nama_sekolah]"?></td>
<td><? echo "$data[jenis_sekolah]"?></td>
<td><? echo "$data[akreditasi]"?></td>
<td><? echo "$data[kecamatan]"?></td>
<td><? echo "$data[alamat]"?></td>
<td><?php echo "<a href='detail.php?id=$data[id]'title='Detail'><u>Selengkapnya</u></a>" ?></td>
<td><?php echo "<a href='map_besar.php?id=$data[id]'title='Peta'><u>Lihat Peta</u></a>" ?></td>
</tr>
<?php $no++; }
echo "</table>";
}
?>
有人能帮我修复上面的代码吗。。我是php和mysql的新手。。CMIIW感谢您的帮助
sql查询看起来不错,但在php中执行的方式有问题。
当您运行mysql_query时,您将在成功时获得一个资源。然后你可以使用这个资源来获取数据,这样你就可以替换
while ($hasil = mysql_query($query)) {
使用以下
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
var $row
将是与所提取的行相对应的字符串的关联数组。因此,您将回声例如$row['nama_sekolah']
我还建议您使用mysqli,因为mysql函数在PHP 5.5.0中被弃用,在PHP 7.0.0中被删除。