DML operations
To execute a DML operation against the database, we can use a set of pg_*
functions. We can use the same functions for all DML operations.
pg_query
/pg_query_params
/pg_insert
/pg_update
/pg_delete
First, let's try to insert records into the database with pg_query
:
$sql=<<<EOF INSERT INTO test VALUES('A', 'B'); INSERT INTO test VALUES('I', 'U'); EOF; if (pg_query($con, $sql)) { echopg_affected_rows($con); echo " Records are inserted successfully"; } else { echo"Failed inserting records"; echopg_last_error($con); echo"\n"; }
Result:
$ php /tmp/test.php
Successfully made connection to PostgreSQL
pg connection pid is 92153
2 Records are inserted successfully
If we want to know how many records our code inserted, then we will have to use pg_affected_rows($con)
after executing the INSERT
statements.
pg_query_params
Using the pg_query_params
function, we can pass...