Get an id *immediately* after creating record?

Not sure of the best way to describe this, but I know what I’m after:

I’ve just created a record and it has a primary key - set to auto-increment.

Immediately after creating that record I need to make use of that id - which of course did not exist moments earlier - to insert a record into another table.

How can I get the result of the first query’s addition to grab that id for use in the second update/insertion? Without having to do some barmy lookup against some other parameter (or a selection of them) that I think are unique enough to give me that newly created record’s id?

Hope this makes sense!

If you are using php , there is a method that retrieves the last insert record id.
http://php.net/manual/en/function.mysql-insert-id.php

if you are doing it in mysql :
http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

Aha, thanks - I’ll read through and digest :slight_smile:

I am using PHP, so I could use either. Is one method preferable to use than another?

Use LAST_INSERT_ID directly in the MySQL query even if you are using PHP. See the PHP manual for details, there are times when using mysql_insert_id() that an incorrect value will be returned.