Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP和Postgres查询的问题_Php_Sql_Postgresql - Fatal编程技术网

PHP和Postgres查询的问题

PHP和Postgres查询的问题,php,sql,postgresql,Php,Sql,Postgresql,我正在尝试使用PHP从postgress表中提取数据。我正在为查询提供参数,虽然我知道返回的表中应该有数据,但返回的数组是空的 <?php ini_set('memory_limit', '1024M'); // Connecting, selecting database $dbconn = pg_connect(***My connection details***) or die('Could not connect: ' . pg_last_erro

我正在尝试使用PHP从postgress表中提取数据。我正在为查询提供参数,虽然我知道返回的表中应该有数据,但返回的数组是空的

<?php
ini_set('memory_limit', '1024M');
      // Connecting, selecting database
$dbconn = pg_connect(***My connection details***)
          or die('Could not connect: ' . pg_last_error());


$lat1 = 50.5;
$lat2 = 58.0;
$lng1 = -3.5;
$lng2 = -7.0;

$colour_points = 'SELECT lat,lng,price,address_string FROM house_price_data_db.main where lat>=$1 and lat<=$2 and lng>=$3 and lng<=$4';
$result_pinpoints_points = pg_query_params($dbconn,$colour_points,array($lat1,$lat2,$lng1,$lng2)) or die('Query failed: ' . pg_last_error());
$pin_points_array = array();

while($r = pg_fetch_assoc($result_pinpoints_points)) {
    $pin_points_array[] = $r;
}

header( "Content-type: application/json" ); // set the header to json 
echo(json_encode(array_values($pin_points_array), JSON_NUMERIC_CHECK)); // this will return json
pg_close($dbconn);
?>
有没有关于如何解决/修复此问题的建议?

只是简单的打字错误

'SELECT lat,lng,price,address_string FROM house_price_data_db.main where lat>=$1 and lat<=$2 and lng**>=**$3 and lng**<=**$4';
应该是:

'SELECT lat,lng,price,address_string FROM house_price_data_db.main where lat>=$1 and lat<=$2 and lng**<=**$3 and lng**>=**$4';

需要更改最后2个输入的相等/小于/大于符号,否则将没有输出。它是说如果>-4和Postgresql附带了一套方便的and。不用在两个字段中存储纬度和经度,您可以使用点类型,更好的是创建一个

使用此选项,您的查询将类似于:

select position, price,address_string FROM house_price_data_db.main where position <@ box($1, $2, $3, $4);
您甚至可以直接在SQL中聚合结果,甚至将其转换为JSON:

with
  result as (
    select 
      array_agg(main) 
    from 
      house_price_data_db.main
    where position <@ box($1, $2, $3, $4)
  )
select row_to_json(result) from result;

您是否尝试直接在数据库上使用参数测试查询?是否可以为$1、$2、$3、$4指定一些值??根据$result\u pintpoints\u points定义的行,这些值被指定为$lat1、$lat2、$lng1、$lng2。我找出了我的错误,并在下面解释。