retrieve auto increment PK from an insert

0 views
Skip to first unread message

Jimboidaho

unread,
Aug 23, 2010, 11:33:12 AM8/23/10
to Professional PHP Developers
I am using Postgres database and PDO for data handling. I have a
table with the PK using "DEFAULT
nextval('directory_directorypk_seq'::regclass)". In another language
I have used I issue an insert and select to get back the PK in one
statement. I tried this in PDO using prepare and got an error that
prepare can only have one statement at a time. I verified in the PG
query window that the syntax works. I think I can do this using a
stored procedure in PG but I was wonder if there was a way in PDO?

Thanks.

Darren Odden

unread,
Aug 23, 2010, 11:36:14 AM8/23/10
to professi...@googlegroups.com
Yes...

--
This group is managed and maintained by the development staff at 360 PSG. An enterprise application development company utilizing open-source technologies for todays small-to-medium size businesses.

For information or project assistance please visit :
http://www.360psg.com

You received this message because you are subscribed to the Google Groups "Professional PHP Developers" group.
To post to this group, send email to Professi...@googlegroups.com
To unsubscribe from this group, send email to Professional-P...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Professional-PHP



--
I like an escalator because an escalator can never break, it can only become stairs. There would never be an escalator temporarily out of order sign, only an escalator temporarily stairs. Sorry for the convenience.
  - Mitch Hedberg

Jimboidaho

unread,
Aug 23, 2010, 11:39:59 AM8/23/10
to Professional PHP Developers
Hint?

Darren Odden

unread,
Aug 23, 2010, 11:43:14 AM8/23/10
to professi...@googlegroups.com

Jimboidaho

unread,
Aug 23, 2010, 11:56:51 AM8/23/10
to Professional PHP Developers
I am embarrassed now. I sure missed that in the help.
Thanks Darren.

On Aug 23, 9:43 am, Darren Odden <dod...@gmail.com> wrote:
> google returnshttp://php.net/manual/en/pdo.lastinsertid.php

Darren Odden

unread,
Aug 23, 2010, 12:00:12 PM8/23/10
to professi...@googlegroups.com
No prob... btw, that is only as good as processing and assumes that your code is the only code inserting... Something to consider if you are moving towards a scalable system so you might want to put in a check that the data returned by the id matches what you inserted.

Robert Gonzalez

unread,
Aug 23, 2010, 12:30:55 PM8/23/10
to professi...@googlegroups.com
Or, for extra sanity, wrap the code in a transaction and grab the insert id before terminating the transaction. Just a thought.

Darren Odden

unread,
Aug 23, 2010, 12:37:26 PM8/23/10
to professi...@googlegroups.com
Another thought would be to setup the sql to query for the returned id of the transaction on the database and then get that returned into your code upon successful completion.
Reply all
Reply to author
Forward
0 new messages