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

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

am 27.11.2007 19:43:29 von Neil Smth

At 23:50 26/11/2007, you wrote:
>Date: Mon, 26 Nov 2007 18:19:07 -0600
>Reply-to: "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 ;-)

Cheers - Neil

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

Re: Re: Subject: Accessing last inserted record in a PSQLDatabase table

am 27.11.2007 23:46:38 von dmagick

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"
>> 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/

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