Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PostgreSQL查询在PHP中创建表?_Php_Html_Mysql_Postgresql - Fatal编程技术网

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

如何使用PostgreSQL查询在PHP中创建表?,php,html,mysql,postgresql,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

我有一些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($\u服务器['REQUEST\u METHOD']=='POST'){
$db_connection=pg_connect(“host=localhost port=5433 dbname=BankeDB user=postgres password=admin”);
echo$db_连接;
$query='选择…(在查询上方)';
$result=pg\u查询($db\u连接,$query,$POST);
$result=pg\u查询($db\u连接,$query);
$DT=阵列('0-10'、'11-50'、'>50',总计);
$result=pg\u查询($db\u连接,$query);
而($DT=pg_fetch_行($result)){
回声“;
回声“$DT[0]”;
回声“$DT[1]”;
回声“$DT[2]”;
回声“$DT[3]”;
回显“$row[4]”;
回声“;
}
回声$结果;
pg_关闭(db);

我认为你能做的最好的事情,就是坐下来(大声地)读给自己听每一行代码都做了什么。记住,它实际上做了什么,而不是你想要它做什么

下面一行就是一个简单的例子:
while($DT=pg_fetch_row($result)){

它指出,只要还有任何行(
pg_fetch_row()
),它就应该提取结果集的下一行(
pg_fetch_row()
)并将其存储到$DT变量中。覆盖存储在该变量中的任何以前的数据(
$DT=

这样做可以让您更好地理解代码的实际功能,使您能够进行一些实际的编程,而不仅仅是向代码扔东西,希望代码能起作用

另一个你应该做的先决条件是对需要采取的步骤进行实际规划。如果你在开始编写代码之前这样做,那么编写代码就会变得容易得多。因为你实际上已经事先解决了大部分问题,所以你只需要将解决方案从简单(速记)翻译过来英语到PHP。
这是通过一种叫做“”的技术来完成的,我强烈建议大家仔细阅读它

也就是说,我已经清理了您的代码并对其进行了评论。为了帮助您开始:

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";
}
if($\u服务器['REQUEST\u METHOD']=='POST'){
//通常,您需要从发布的代码中删除连接详细信息。
$db_connection=pg_connect(“host=localhost port=5433 dbname=BankeDB user=postgres password=admin”);
$query='选择…(在查询上方)';
//在这里,您实际上连续运行了三次查询,
//每次都覆盖上一个结果。
//还有,为什么是“$POST”?
$result=pg\u查询($db\u连接,$query,$POST);
$result=pg\u查询($db\u连接,$query);
$result=pg\u查询($db\u连接,$query);
//用于保存输出的临时变量,而不是直接(不可恢复地)将其发送到浏览器。
//使用herdoc语法,使内容更易于阅读。
//您要在这里写出标题,因为它们位于自己的行中。(TR==表行)

$htmlOut=请发布您尝试过的内容,以及遇到的任何错误。另外,请提供一个您想要的输出示例。我对php非常熟悉,所以不知道如何开始。我已经设置了数据库连接,并且工作正常。我已经创建了数组以在web中创建表(使用php)@ChristianF我已经补充了一些我到目前为止所做的事情。你能帮我整理一下吗?php的结果是什么?
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";
}