简体   繁体   中英

Find Nearest Place from MySQL

I want to see the nearby locations. I want to see the most nearby locations. I want to calculate it based on latitude and longitude. But I'm getting this error.Thank you. Comments below is works, but I'm getting a 'json parse error: unable to parse json string' error when pulling php file with react native. I updated the topic.

React Native Code

fetch('http://.../KONUM.php')
    .then((response) => response.json())
    .then((responseJson) => {
        firebase.database().ref('/KONUM/').set(responseJson);
    })
    .catch((error) => {
        console.error(error);
    });

'Notice: Undefined variable: json in /home/.../public_html/database/...php on line 32'

Here is my code:

ID = 4
ADI_SOYADI = STAK NEW
ADRES = 77.2413969039917
ACIK_ADRES = 28.653838307772872

<?php
include 'DBConfig.php';

// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

if ($conn->connect_error) {  
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT ID,ADI_SOYADI, ( 3959 * acos( cos( radians(37) ) * cos( radians( ADRES ) ) * cos( radians( ACIK_ADRES ) - radians(-122) ) + sin( radians(37) ) * sin( radians( ADRES ) ) ) ) AS distance FROM komutlab_soyle_gelsin.TBL_MUSTERILER HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row[] = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);
    }
} else {
    echo "No Results Found.";
}

echo $json;
$conn->close();
?>

You should define $json outside the scope of the if-else expression:

$json = NULL;
if ($result->num_rows > 0) {
    while($row[] = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);
    }
}
else {
    echo "No Results Found.";
}
echo $json;

But most likely you would only want to be working with your JSON if all the SQL logic proceeded without error. So, in this case, I might just move the echo call to inside the if condition:

if ($result->num_rows > 0) {
    while($row[] = $result->fetch_assoc()) {
        $tem = $row;
        $json = json_encode($tem);
        echo $json . "\n";
    }
}
else {
    echo "No Results Found.";
}

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