Nathaniel Price wrote:
----- Original Message -----
From: "Roger 'Rocky' Vetterberg" <[EMAIL PROTECTED]>
To: "kevin myers" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Monday, March 31, 2003 4:52 AM
Subject: Re: [PHP-DB] getting serial in PHP


What I do, when I need to reference an ID from a sequence after I do the
INSERT, is I get the next number in the sequence /before/ inserting the data
into the table, like so:

$res = pg_query($conn, "select nextval('my_seq');");
$row = pg_fetch_array($res);
$id = $row['nextval'];
$res = pg_query($conn, "insert into my_table values ($id, 'value',
//Error checking here...
echo "Your submission was accepted with ID $id";

Since you are just getting the next value of the sequence, your data is safe
from having non-unique id numbers, as long as you always call nextval() in
order to get your id number. Any subsequent calls to nextval() will return
the next number, regardless of whether you use the $id returned by the first

Hope that helps.

Nathaniel Price
"Who is General Failure and why is he reading my hard drive?"

So even if I just do a select on nextval, it increases in value?
I was under the impression that a select would just return the current value, and that I would have to do a insert to actually increase it?

I will certainly test this, it sounds like this could be the solution Im looking for.



PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to