Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
1  
1  
The docs (php.net/pg_query) show you how to iterate a resultset. –  Anthony Sterling Dec 18 '13 at 11:46

1 Answer 1

up vote 1 down vote accepted

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");
    }
share|improve this answer
    
Thank you loads. I have to read the documentation on PHP.net more carefully next time. –  MichaelP Dec 18 '13 at 12:01
1  
Happy to help.... :) –  Dinesh Dec 18 '13 at 12:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.