简体   繁体   中英

find nearest location using SQL

I want to find nearest location saved in my database table

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

i have tried with this code bellow, but with no one result nearestCat is checkbox and $terdekat is input text for distance in 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>";
     }

    ?>

can anyone help me to fix my code above.. i'm newbie in php n mysql.. CMIIW thanks for your help

The sql query seems fine, but there is a problem with the way it is executed in php.

When you run the mysql_query you get a resource on success. You can then use this resource to fetch the data, so you would replace

while ($hasil = mysql_query($query)) {

With the following

$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {

The var $row will be an associative array of strings that corresponds to the fetched row. So you would echo for example $row['nama_sekolah']

I'd also recommend you use mysqli since mysql functions were deprecated in PHP 5.5.0, and it removed in PHP 7.0.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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