note 29293 modified in function.pg-last-oid by jsheets

From: Date: Sun, 09 Feb 2003 02:13:43 +0000
Subject: note 29293 modified in function.pg-last-oid by jsheets
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-43670@lists.php.net to get a copy of this message
[Editor's Note: If another record is inserted after the nextval is obtained and before you the INSERT query this code will fail. This should not be done on busy sites.] Especially now with an optional oid field, getting an implicit serial key is harder than ever. The solution is to get the serial key first, and then use that value in an insert: <? pg_query($conn, 'CREATE TABLE foo (key SERIAL, foo TEXT)'); $res=pg_query("SELECT nextval('foo_key_seq') as key"); $key=pg_fetch_array($res, 0); $key=$key[key]; // now we have the serial value in $key, let's do the insert pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')"); ?> Hope this helps... --was-- Especially now with an optional oid field, getting an implicit serial key is harder than ever. The solution is to get the serial key first, and then use that value in an insert: <? pg_query($conn, 'CREATE TABLE foo (key SERIAL, foo TEXT)'); $res=pg_query("SELECT nextval('foo_key_seq') as key"); $key=pg_fetch_array($res, 0); $key=$key[key]; // now we have the serial value in $key, let's do the insert pg_query("INSERT INTO foo (key, foo) VALUES ($key, 'blah blah')"); ?> Hope this helps... http://www.php.net/manual/en/function.pg-last-oid.php

« previous php.notes (#43670) next »