2016-09-16 56 views
-1

我有一些PostgreSQL查询,其输出如下所示。现在我想显示该输出查询PHP如何做到这一点? 我的PostgreSQL的查询如下:如何使用PostgreSQL查询在PHP中创建表?

$query = 'select 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 0 and 10) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) "0-10", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 11 and 50) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) "11-50", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" >50) then 1 else 0 end))/(sum(case when "WELLTYPE"= 'DT' then 1 else 0 end)),1) ">50", 
    round(
    100.00 * 
    (sum(case when "WELLTYPE"= 'DT' and ("CONC_ARSC" between 0 and 10) then 1 else 0 end))/(sum(case when ("WELLTYPE"= 'DT' or "WELLTYPE"= 'DW' or "WELLTYPE"= 'FT' or "WELLTYPE"= 'ST') and ("CONC_ARSC" between 0 and 10)then 1 else 0 end)),1) "Total" 
    from public."Arsenic_Test"'; 

上述PostgreSQL的查询的输出是这样的:

____________________________________________ 
|0_to_10| 11_to_50 | greater_than_50 | Total| 
--------+----------+-----------------+------| 
| 100 | 0.0 | 0.0   | 0.4 | 
---------------------------------------------- 

我是在PHP非常beginer所以我不知道如何下手。我有安装数据库连接和它的工作正常。我已经(使用PHP)

if($_SERVER['REQUEST_METHOD'] == 'POST'){ 

    $db_connection = pg_connect("host=localhost port=5433 dbname=BankeDB user=postgres password=admin"); 
     echo $db_connection; 
    $query = 'select.... (above query) '; 
$result = pg_query($db_connection, $query, $POST); 
    $result = pg_query($db_connection, $query); 
$DT = array('0-10','11-50','>50',total); 
    $result = pg_query($db_connection, $query); 
         while ($DT = pg_fetch_row($result)) { 
       echo "<tr>"; 
         echo "<td>$DT[0]</td>"; 
         echo "<td>$DT[1]</td>"; 
         echo "<td>$DT[2]</td>"; 
         echo "<td>$DT[3]</td>"; 
         echo "<td>$row[4]</td>"; 
       echo "</tr>"; 
       } 

    echo $result;    
       pg_close($db); 
+0

请发布您尝试过的以及您遇到的任何错误。此外,你想要的输出的例子会很好。 – ChristianF

+0

我很开心在PHP中,所以我不知道如何开始。我有安装数据库连接和它的工作正常。我创建了一个数组来创建表格(使用php) – Ram

+0

@ChristianF我已经添加了一些我迄今为止所做的信息。你能帮我解决吗? – Ram

回答

0

我认为你能做的最好的事情,就是坐下来阅读(大声)对自己做些什么的代码的每个行则创建阵列创建的Web表格。它实际上做了什么,而不是你想要做什么,请关注你。

这方面的一个简单的例子,是以下行: while ($DT = pg_fetch_row ($result)) {
它规定,即只要有左(whilepg_fetch_row())任何行,那么就应该取结果集的下一行( pg_fetch_row())并将其存储到$ DT变量中。 覆写存储在该变量的任何以前的数据($DT =

这样做会给你一个更好的了解你的代码实际上呢,让你做一些实际的编程,而不仅仅是它扔东西,希望东西棒(作品)。

您应该继续努力的另一个先决条件是需要采取的步骤的实际计划。如果您在开始编写代码之前执行此操作,编写代码将变得更加容易。事实上,事实上你已经解决了大部分问题,所以你只需要将解决方案从普通(速记)英语翻译成PHP。
这是用一种叫做“pseudo code”的技术完成的,我强烈建议阅读它!

这就是说,我已经清理了一下你的代码并对它进行了评论。为了帮助您开始:

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    // Normally you'll want to remove the connection details from the code you post. 
    $db_connection = pg_connect ("host=localhost port=5433 dbname=BankeDB user=postgres password=admin"); 

    $query = 'select.... (above query) '; 

    // Here you're actually running the query three times in a row, 
    // overwriting the previous result each time. 
    // Also, why "$POST"? 
    $result = pg_query ($db_connection, $query, $POST); 
    $result = pg_query ($db_connection, $query); 
    $result = pg_query ($db_connection, $query); 

    // Temporary variable to hold the output, instead of directly (and irreverably) sending it to the browser. 
    // Using the HereDOC syntax, to make things a bit easier to read. 
    // It's here you want to write out the headers, as they're in their own row. (TR == table row) 
    $htmlOut = <<<outHTML 
<table> 
    <thead> 
     <tr> 
      <th></th> 
      .... 
     </tr> 
    </thead> 
    <tbody> 
outHTML; 

    // Why not just add the table header cells to the output directly? 
    $DT = array ('0-10', '11-50', '>50', total); 

    // As you're overwriting (deleting) the contents of above array here anyway. 
    while ($DT = pg_fetch_row ($result)) { 
     // Again, hereDOC syntax for easier reading. 
     // Though, where do you get the "$row" variable from? 
     $htmlOut .= <<<outHTML 
     <tr> 
      <td>{$DT[0]}</td> 
      <td>{$DT[1]}</td> 
      <td>{$DT[2]}</td> 
      <td>{$DT[3]}</td> 
      <td>{$row[4]}</td> 
     </tr> 
outHTML; 
    } 

    // $result is a PG resource, so I assume this is for debugging purposes only. 
    // echo $result; 

    // You'll want to print out the completed array instead. 
    echo $htmlOut."\t</tbody>\n</table>\n"; 
} 

PS:我也建议考虑PDO,而不是使用旧pg_*()功能。