简体   繁体   English

PHP SQL st_within

[英]PHP SQL st_within

When i run this query in phpMyAdmin it works: 当我在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))))

it gives me a list of the points that are within those boundaries, but if i run this from a php file: 它给了我在这些边界内的点的列表,但是如果我从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);
}
}
?>

i get this: 我得到这个:

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

if i run: $sql = "SELECT *FROM person" from the php file it works. 如果我运行:$ sql =“ SELECT * FROM person”从php文件中起作用。

is there a problem with st_within on this php version? 这个php版本的st_within有问题吗?

PHP version 5.5.12 SQL 5.6.17 PHP版本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: 尝试替换为$ rlon1 / $ rlat1 / $ rlon2 / $ rlat2:

$sql = "SELECT *FROM person WHERE st_within(point(longitude, latitude), envelope(linestring(point('$rlon1', '$rlat1'), point('$rlon2', '$rlat2'))))"; $ sql =“ SELECT *从某人那里st_within(point(经度,纬度),信封(linestring(point('$ rlon1','$ rlat1'),point('$ rlon2','$ rlat2'))))) “;

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

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