繁体   English   中英

使用php postgres从表中选择特定列

[英]To select a specific column from a table using php postgres

我有5000+行和8+列的表格,

Station Lat Long    Date    Rainfall    Temp    Humidity    Windspeed
Abcd    -   -   09/09/1996  -   -   -   -
Abcd    -   -   10/09/1996  -   -   -   -
Abcd    -   -   11/09/1996  -   -   -   -
Abcd    -   -   12/09/1996  -   -   -   -
Efgh    -   -   09/09/1996  -   -   -   -
Efgh    -   -   10/09/1996  -   -   -   -
Efgh    -   -   11/09/1996  -   -   -   -
Efgh    -   -   12/09/1996  -   -   -   -

我正在开发一个Web应用程序,该用户将在特定日期选择一列,例如降雨/温度/湿度。

谁能指导我如何在php-postgres中对此进行查询。 (数据库:postgres,表:天气数据,用户:user,密码:password)

提前致谢。

您可以使用如下代码:

public function getData ($date, $columnsToShow = null) {

  /* You could check the parameters here:
   *   $date is string and not empty
   *    $columnsToShow is an array or null.
   */ 

  if (isset ($columnsToShow))
    $columnsToShow = implode (',', $columnsToShow);
  else  $columnsToShow = "*";

  $query = "select {$columnsToShow}
            from table
            where date = '{$date}'";

  $result = array();
  $conex = pg_connect ("host=yourHost user=yourUser password=yourUser dbname=yourDatabase");
  if (is_resource ($conex)) {
    $rows = pg_query ($conex, $query);

    if ($rows) {
      while ($data = pg_fetch_array ($rows, null, 'PGSQL_ASSOC'))
         $result[] = $data;
    }
  }
  return (empty ($result) ? null : $result);
}

现在,您可以像这样调用:

getData ('2012-03-21', array ('Station', 'Rainfall'));

希望您能为您服务。

暂无
暂无

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

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