[solved] return current row auto_increment value on insert?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

[solved] return current row auto_increment value on insert?

Post by Skara »

example table:

id, int, auto_inc, primary key
data1, int
data2, varchar
...etc

Can I get the value of `id` when I insert a row somehow? Here's what I've currently got (basically):

Code: Select all

INSERT INTO `table` VALUES('','abc','def','ghi');
SELECT `id` FROM `table` WHERE `data1`='abc' AND `data2`='def' AND `data3`='ghi' LIMIT 1;
--there won't be any duplicate entries, so this /should/ work, but..

I'd LIKE to do something like...

Code: Select all

INSERT INTO `table` VALUES('','abc','def','ghi') AND SELECT `id` FROM {this};
Is something like that possible?
Last edited by Skara on Tue Feb 20, 2007 4:45 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

SELECT LAST_INSERT_ID()
or mysql_insert_id() maybe?
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

aha! Thanks!
Post Reply