如何使用PostgreSQL查询在PHP中创建表?

时间:2016-09-16 06:34:44

标签: php html mysql postgresql

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

   $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,所以我不知道如何开始。我有设置数据库连接和它的工作正常。我创建了数组以在web中创建表(使用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);

1 个答案:

答案 0 :(得分:0)

我认为你能做的最好的事情就是坐下来(大声)读出每个代码行所做的事情。不管你想要它做什么,请注意你。

这方面的一个简单示例是以下行: while ($DT = pg_fetch_row ($result)) {
它指出,只要剩下任何行(whilepg_fetch_row()),它就应该获取结果集的下一行(pg_fetch_row())并将其存储到$ DT变量。 覆盖存储在该变量($DT =

中的所有先前数据

这样做可以让你更好地理解你的代码实际上做了什么,使你能够做一些实际的编程,而不仅仅是扔东西,希望有些东西可以粘贴(有效)。

您应该处理的另一个先决条件是实际规划需要采取的步骤。如果在开始编写代码之前执行此操作,则编写代码会变得更加容易。看到你事先已经解决了大部分问题,所以你只需要将解决方案从普通(简写)英语翻译成PHP。
这是通过一种名为&#34; pseudo code&#34;的技术完成的,我强烈建议您阅读它!

那就是说,我已经清理了你的代码并对其进行了评论。为了帮助您入门:

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_*()函数。