Re: getting serial in PHP

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 





Nathaniel Price wrote:
----- Original Message -----
From: "Roger 'Rocky' Vetterberg" <listsub@401.cx>
To: "kevin myers" <kevandy1386@msn.com>; <php-db@lists.php.net>
Sent: Monday, March 31, 2003 4:52 AM
Subject: Re:  getting serial in PHP

*snip*


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',
'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
query.

Hope that helps.

________________________________________
Nathaniel Price http://www.thornvalley.com
"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.

Thanks!

--
R



--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux