简体   繁体   中英

PHP SQL st_within

When i run this query in phpMyAdmin it works:

set @lat= xxxxxx;
set @lon = -xxxxxx;
set @dist = xxxx;

set @rlon1 = @lon-@dist;
set @rlon2 = @lon+@dist;
set @rlat1 = @lat-@dist;
set @rlat2 = @lat+@dist;


select * from person where st_within(point(longitude, latitude), envelope(linestring(point(@rlon1, @rlat1),        point(@rlon2, @rlat2))))

it gives me a list of the points that are within those boundaries, but if i run this from a php file:

<?php

if (isset($_GET['latitude']) && isset($_GET['longitude']) && isset($_GET['radius'])) {   

$lat = $_GET['latitude'];
$lon = $_GET['longitude'];
$dist = $_GET['radius'];

$rlon1 = $lon-$dist;
$rlon2 = $lon+$dist;
$rlat1 = $lat-$dist;
$rlat2 = $lat+$dist;

require_once __DIR__ . '/db_connect.php';

$db = new DB_CONNECT();
$db->connect();

$sql = "SELECT *FROM person WHERE st_within(point(longitude, latitude), envelope(linestring(point('rlon1', 'rlat1'), point('rlon2', 'rlat2'))))";

$row = $db->query($sql);

if ($row != null) {
// looping through all results
// persons node
$response["persons"] = array();

while ($result = mysqli_fetch_array($row)) {
    // temp user array
    $person = array();
    $person["name"] = $result["name"];
    $person["latitude"] = $result["latitude"];
    $person["longitude"] = $result["longitude"];

    // push single person into final response array
    array_push($response["persons"], $person);
 }
 // success
 $response["success"] = 1;

 // echoing JSON response
 echo json_encode($response);
} else {
// no persons found
$response["success"] = 0;
$response["message"] = "No persons found";

// echo no users JSON
echo json_encode($response);
}
}
?>

i get this:

{"persons":[],"success":1}

if i run: $sql = "SELECT *FROM person" from the php file it works.

is there a problem with st_within on this php version?

PHP version 5.5.12 SQL 5.6.17

$sql = "SELECT *FROM person WHERE st_within(point(longitude, latitude), envelope(linestring(point('rlon1', 'rlat1'), point('rlon2', 'rlat2'))))";

Looks like you're missing the dollar signs.

Try replacing with $rlon1 / $rlat1 / $rlon2 / $rlat2:

$sql = "SELECT *FROM person WHERE st_within(point(longitude, latitude), envelope(linestring(point('$rlon1', '$rlat1'), point('$rlon2', '$rlat2'))))";

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