繁体   English   中英

使用SQL查找最近的位置

[英]find nearest location using SQL

我想找到保存在数据库表中的最近位置

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是在KiloMeters中输入距离的文本

<?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 n 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中已将其删除。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM