문제

I'm trying to do a simple query from my postgres database, then try to manipulate the data.

Script I made goes as following;

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");
    }

Now everything goes fine, except the printing only prints out the first row of the Database result set, while I have 3 rows in my database. What am I missing here?

도움이 되었습니까?

해결책

pg_fetch_array() returns an array that corresponds to the fetched row (record) if you have more than 1 record in table you should use while loop

as:

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top