Re: Re: Subject: Accessing last inserted record in a PSQL Database table

From: Date: Tue, 27 Nov 2007 22:46:38 +0000
Subject: Re: Re: Subject: Accessing last inserted record in a PSQL Database table
References: 1 2  Groups: php.db 
Request: Send a blank email to php-db+get-44365@lists.php.net to get a copy of this message
Neil Smith [MVP, Digital media] wrote:
At 23:50 26/11/2007, you wrote:
Date: Mon, 26 Nov 2007 18:19:07 -0600 Reply-to: "kdealba@uaaan.mx" <kdealba@uaaan.mx> Message-ID: <8ceba9b15b29b1d0eb505ceecb697d28@correo.uaaan.mx> I need to access the last inserted record in a postgresql database table from php. I connect to the db using pg_connect I construct the query string, and I use pg_query to send the query. I get the record inserted, of course. One of the fields, which is my primary key, is an autoincrement field. I need to fetch this value. Any idea? I would really apreciate your helping me.
SELECT primary_key_name FROM database_table ORDER BY primary_key_name DESC Just use whatever cursor or LIMIT you can in Postgres to get a single record. If it's autoincrement, then the last record ordered in descending order is, the last record ;-)
Not unless you wrap this (and the rest of your insert) in a transaction. session 1: adds a record session 2: adds a record session 1: tries to fetch last primary key - gets it wrong. -- Postgresql & php tutorials http://www.designmagick.com/

« previous php.db (#44365) next »