$query1 = pg_query('SELECT "id","location",ST_Distance_Sphere(ST_Point(lat,long ), ST_Centroid("path"::geometry)) FROM places
ORDER BY ST_Distance_Sphere
LIMIT 1');
echo $query1
我有一个带有lat和long coardinates的数据库,如果我在phpPgAdmin中运行上面的查询它给了我想要的结果,但是当通过php查询时它没有,它给我“资源7”而不是
我是否正确格式化了上面的查询?
提前感谢AJ。
答案 0 :(得分:0)
通过将SQL语句分配给变量,然后回显出变量的值,可以判断SQL语句是否格式正确。您需要回显SQL语句,而不是pg_query()返回的值。
$ cat code/php/test.php
<?php
$sql = 'SELECT "id","location",ST_Distance_Sphere(ST_Point(lat,long ), ST_Centroid("path"::geometry)) FROM places ORDER BY ST_Distance_Sphere LIMIT 1';
echo $sql;
$ php code/php/test.php
SELECT "id","location",ST_Distance_Sphere(ST_Point(lat,long ), ST_Centroid("path"::geometry)) FROM places ORDER BY ST_Distance_Sphere LIMIT 1% ➜ ~
ST_Point()的参数顺序是“long,lat”,而不是“lat,long”。如果您的原始查询返回正确的结果,我会有点担心。
答案 1 :(得分:0)
我认为问题是echo $ query1
你不能只是回应结果。首先你必须声明一个连接然后你必须用它做一些事情才能打印出来。例如:
$con = pg_connect("host=you_host_db port=5432 dbname=database_name");
$query1 = pg_query($con, 'SELECT "id","location",ST_Distance_Sphere(ST_Point(lat,long ) as a,ST_Centroid("path"::geometry)) as b FROM places ORDER BY ST_Distance_Sphere LIMIT 1');
while ($row = pg_fetch_assoc($query1)) {
echo $row['id'];
echo $row['location'];
echo $row['a'];
echo $row['b'];
}