2016-09-16 48 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_*()功能。

相關問題