【问题标题】:To select a specific column from a table using php postgres使用 php postgres 从表中选择特定列
【发布时间】:2012-04-05 23:18:13
【问题描述】:

我有 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,表:天气数据,用户:用户,密码:密码)

提前致谢。

【问题讨论】:

    标签: php postgresql


    【解决方案1】:

    您可以使用如下代码:

    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'));
    

    我希望你服务。

    【讨论】:

      猜你喜欢
      • 2021-10-27
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-12-01
      • 2019-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      相关资源
      最近更新 更多