【发布时间】:2014-04-10 20:12:58
【问题描述】:
我正在尝试在 PHP 文件中使用 PostgreSQL/PostGIS 查询中的变量。
变量需要将 lat 和 lng 值放在 POINT 几何图形中,但在我的代码中有些东西不能正常工作,我收到错误消息:
警告:pg_query():查询失败:错误:解析错误 - 无效 geometry HINT: "POINT("
当我运行具有 POINT 值(而不是变量)的查询时
SELECT ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT(-85.1043 34.315)',4326)), 0.1));
一切正常。
我在网上看了一些文章和帖子,但我找不到解决办法:
我在 php 文件中使用的整个代码如下:
<?php
$host = "localhost";
$user = "postgres";
$pass = "2907";
$db = "postgis";
$intptlon = pg_escape_string($_GET['intptlon']);
$intptlat = pg_escape_string($_GET['intptlat']);
function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','<',$htmlStr);
$xmlStr=str_replace('>','>',$xmlStr);
$xmlStr=str_replace('"','"',$xmlStr);
$xmlStr=str_replace("'",''',$xmlStr);
$xmlStr=str_replace("&",'&',$xmlStr);
return $xmlStr;
}
// Opens a connection to a PostgreSQL
$con = pg_connect("host=$host dbname=$db user=$user password=$pass") or die ("Could not connect to server\n");
$query = "select ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT('$intptlon' '$intptlat')',4326)), 0.1))";
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = @pg_fetch_assoc($rs)){
// ADD TO XML DOCUMENT NODE
echo '<marker ';
echo 'name="' . parseToXML($row['name']) . '" ';
echo 'intptlat="' . $row['intptlat'] . '" ';
echo 'intptlon="' . $row['intptlon'] . '" ';
echo 'the_geom="' . $row['location'] . '" ';
echo '/>';
}
echo '</markers>';
?>
【问题讨论】:
-
尝试将
POINT('$intptlon' '$intptlat')更改为POINT($intptlon $intptlat)或更好 - 使用参数进行查询。 -
我尝试使用参数进行查询,并将我的查询更改为
$rs = pg_query_params($con,"select ST_AsText (the_geom) as location, name, intptlat, intptlon FROM tiger_match WHERE ST_INTERSECTS (the_geom, ST_Buffer((ST_GeomFromText('POINT($intptlon $intptlat)',4326)), 0.1))");,但我现在收到错误pg_query_params() expects parameter 1 to be string。这部分代码是不是也需要改一下$intptlon = pg_escape_string($_GET['intptlon']);才能使用参数查询?
标签: php postgresql postgis