繁体   English   中英

打印Postgres数据库数组中的所有行

[英]Printing all rows from a Postgres database array

我试图从我的postgres数据库中做一个简单的查询,然后尝试操纵数据。

我编写的脚本如下:

function connectLocalDB() {

    $dbconnection = pg_connect("host=192.168.97.120 port=1337 dbname=x user=x password=x") or die("Unable to connect to Postgres");


    // INPUT table from userDB
    $userINPUTresult = pg_query($dbconnection, "SELECT * FROM \"INPUT\"");
    if (pg_num_rows($userINPUTresult)>0) {

        $userINPUTArray = pg_fetch_array($userINPUTresult);
        print_r($userINPUTArray);

        echo "INPUT CHAIN RULES LOADED \n";

    } else {

        echo ("NO INPUT CHAIN RULES \n");
    }

现在一切正常,只是打印只打印出数据库结果集的第一行,而我的数据库中只有3行。 我在这里想念什么?

如果表中有多个记录,则pg_fetch_array()返回与获取的行(记录)相对应的数组,您应该使用while循环

如:

if (pg_num_rows($userINPUTresult)>0) {

        while($userINPUTArray = pg_fetch_array($userINPUTresult))
        {
        print_r($userINPUTArray);

        echo "INPUT CHAIN RULES LOADED \n";
        }
    } else {

        echo ("NO INPUT CHAIN RULES \n");
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM