PHP SQL st_within

时间:2014-06-22 05:01:22

标签: php sql

当我在phpMyAdmin中运行此查询时,它可以工作:

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))))

它给了我一个列在这些边界内的点,但是如果我从一个php文件中运行它:

<?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);
}
}
?>

我明白了:

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

如果我运行:$ sql =“SELECT * FROM person”来自它运行的php文件。

这个php版本的st_within有问题吗?

PHP版本5.5.12 SQL 5.6.17

1 个答案:

答案 0 :(得分:1)

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

看起来你错过了美元符号。

尝试用$ rlon1 / $ rlat1 / $ rlon2 / $ rlat2替换:

$ sql =“SELECT * FROM person WHERE st_within(point(经度,纬度),信封(linestring(point('$ rlon1','$ rlat1'),point('$ rlon2','$ rlat2') )))“;