错误连接到postgres(pg_close)

时间:2018-02-13 14:27:43

标签: php postgresql php-pgsql

我有以下代码:

  require 'dataload.php';
  function get_objects($where,$name=false) {

    global $epsg, $cnt_array;

    $db = Dataload::getDB();

    $columns="osm_id, ST_AsGeoJSON(ST_Transform(way,4326)) as way2, name, ward, \"healthcare:speciality\", information, description, social_facility, \"social_facility:for\", capacity, operator, official_name, official_status, phone, website, \"addr:full\", \"addr:city\", \"addr:district\", \"addr:postcode\", opening_hours, \"addr:hamlet\", \"addr:street\", fax, email, allhuman, adulthuman, childhuman, \"healthcare:heart\", \"healthcare:mind\", \"healthcare:maternity_light\", \"healthcare:maternity_hard\", \"healthcare:dtp\", \"ward:speciality_gynaecology\", \"ward:speciality_maternity\", \"ward:speciality_infectious_diseases\", \"ward:speciality_neurology\", \"ward:speciality_paediatrics\", \"ward:speciality_general\", \"ward:speciality_surgery\", \"internet_access:operator\", \"internet_access:speed\", \"wifi_access:ssid\"";
    $query="select ".$columns." from test_point where ".$where;

    $result = pg_query($query);
    if (!$result) {
        echo "Problem with query " . $query . "<br/>";
        echo pg_last_error();
        exit();
    }

    $geojson = array(
                     'type'      => 'FeatureCollection',
                     'features'  => array(),
                     'crs' => array(
                                    'type' => 'EPSG',
                                    'properties' => array('code' => '4326')
                     )
    );
    while($myrow = pg_fetch_assoc($result)) {

    $gos18_work = array();
    if($name=="gos18") {
        $query_gos18_work = "select * from gos18_work where obj=".$myrow["osm_id"];
        $result_gos18_work = pg_query($query_gos18_work);
        if (!$result_gos18_work) {
            echo "Problem with query " . $query_gos18_work . "<br/>";
            echo pg_last_error();
            exit();
        }
        while($myrow_gos18 = pg_fetch_assoc($result_gos18_work)) {
            $gos18_work[] = array(
                blah=>blah
            );
        }
    }

          $feature = array(
                           'type' => 'Feature',
                           'id' => $myrow["osm_id"],
                           'layer' => $epsg,
                           'geometry' => json_decode($myrow["way2"], true),
                           'geometry_name' => 'way',
                           'properties' => array(
                                                 'name' => $myrow["name"],
                      )
          );
          // Add feature array to feature collection array
          array_push($geojson['features'], $feature);

    }

    // Close database connection
    pg_close($db);

 }
 if(blah) ......get_objects($where);....

Dataload(获取DB的类):

   public static function getDB() {
    return pg_connect('host=notlocalhost port=5432 user=user password=password dbname=dbname') 
        or die("not connect".pg_last_error());
}

如果数据库连接到localhost,但另一台服务器(没有数据库的复制站点)返回错误:

  

警告:pg_close()期望参数1是资源,布尔值   在my.php中给出了....

BUT!如果评论pg_close()此工作没有任何错误并返回DB的结果。

1 个答案:

答案 0 :(得分:1)

pg_connect的php手册评论:

  

小心写一些像

这样的东西
<?php
function getdb_FAILS() {
    return pg_connect("...") or die('connection failed');
}
?>
     

它将返回一个布尔值。如果你不这样做,这似乎没问题   使用返回值作为数据库连接句柄,但如果你将失败   做。

     

相反,请使用:

<?php
function getdb() {
    $db = pg_connect("...") or die('connection failed');
    return $db;
}
?>
     

实际上返回一个句柄。

希望这有帮助。