note 42788 added to function.pg-last-oid

From: Date: Sat, 29 May 2004 09:46:03 +0000
Subject: note 42788 added to function.pg-last-oid
Groups: php.notes 
Request: Send a blank email to php-notes+get-70270@lists.php.net to get a copy of this message
If you want to get the value of a sequence out of pgsql, similar to mysql_insert_id(), try this: <? pg_query( $connection, "BEGIN TRANSACTION" ); pg_query( $connection, $your_insert_query ); $result = pg_query( $connection, "SELECT CURRVAL('$seq_name') AS seq" ); $data = pg_fetch_assoc( $result ); pg_free_result( $result ); pg_query( $connection, "COMMIT TRANSACTION" ); echo "Insert sequence value: ", $data[ 'seq' ], "<BR>\n"; ?> Note the transaction that groups the INSERT query and the query that obtains the sequence value. If you are already in a transaction, don't start another. Also be aware that CURRVAL only works after an INSERT query, and always returns the value for the last INSERT for your connection, even if someone else has done an INSERT after you. See: http://www.postgresql.org/docs/7.3/interactive/functions-sequence.html ---- Manual Page -- http://www.php.net/manual/en/function.pg-last-oid.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42788 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42788&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42788&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#70270) next »