Sometimes you may want to get nextval from several sequences at the same time, i.e. if fields
are not set to auto increment and you want to run transaction from php script, this will save
calls to pg_query and pg_fetch_array. Here's one of the ways to do it in PostgreSQL and PHP.
/* In Postgres database create table */
CREATE TABLE dual (
dummy varchar(1)
);
INSERT INTO dual (dummy) VALUES ('X');
/* In PHP script use this code */
$seq_sql = "SELECT *
FROM
(SELECT nextval('seq1') AS id1 FROM dual) a,
(SELECT nextval('seq2') AS id2 FROM dual) b,
(SELECT nextval('seq3') AS id3 FROM dual) c";
$result = pg_query($conn, $sql);
$rows_returned = pg_NumRows($result);
if ($rows_returned > 0) {
for ($i=0; $i < $rows_returned; $i++) {
$ids = pg_fetch_array($result, $i);
}
}
|