0

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?

2

1 Answer 1

3

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");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you loads. I have to read the documentation on PHP.net more carefully next time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.