Neil Smith [MVP, Digital media] wrote:
At 23:50 26/11/2007, you wrote:
Date: Mon, 26 Nov 2007 18:19:07 -0600

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

PHP Database Mailing List (
To unsubscribe, visit:

Reply via email to