1

I'm writing a code with this function:

function foo() {

    $input = array('bar');

    for ($i=0;$i<1000;$i++) {

        $db=connection_pgsql() or die('Connessione al DBMS non riuscita');

            pg_prepare( "run","INSERT INTO tab1(name) VALUES  ($1)");
            pg_execute("run",$input);

            pg_prepare( "run","INSERT INTO tab2(name) VALUES  ($1)");
            pg_execute("run",$input);

            pg_prepare( "run","INSERT INTO tab3(name) VALUES  ($1)");
            pg_execute("run",$input);

        pg_close($db);


    }
}   

After the execution of this function there are only

20 rows in tab1, 20 rows in tab2 and 20 rows in tab3

and not

1000 rows in tab1, 1000 rows in tab2 and 1000 rows in tab3.

Is it a problem of php or postgresql configuration?

1
  • I don't know php but you need to inspect the results of those inserts. Could be failing a constraint or something Commented Dec 11, 2016 at 2:27

2 Answers 2

0

You're not checking for errors.

Check the return value of functions and use the error info functions provided by PHP.

See:

You can also check the PostgreSQL error logs.

It's much clearer if you use PDO; see e.g. How is a Postgres "RAISE EXCEPTION" converted into a PDOException? .

Also, don't open and close connections each loop iteration like this, the performance will be awful. Preferably do all the work in a single transaction on a single connection.

Sign up to request clarification or add additional context in comments.

4 Comments

I printed for each row last error, result error and affecter rows. Last error and result error are nulls, affected rows is 1. I try with only 1 table insert in a for of 1000 statements. Only 130 are execute...
please update your question then (preferably with smaller iteration number - eg loops over 20 times, inserts only 19 rows) and result error is null or so.
Don't print the results, check for errors exit the loop. You can easily miss something when you're reading 1000 lines of output.
there aren't error in loop. It only stops after n rows.
0

check the resource returned or at least if it is not false. Look at docs example, you should not just pg_execute("run",$input);, but

$result = pg_execute("run",$input);

so you can run

var_dump($result);

This way you will see full dump of value returned on your html page.

1 Comment

var_dump($result) returns: resource(number of $i) of type (pgsql result)

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.