lloydi — 2010-11-02T14:49:15-04:00 — #1
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!
hostentic — 2010-11-02T14:59:00-04:00 — #2
If you are using php , there is a method that retrieves the last insert record id.
if you are doing it in mysql :
lloydi — 2010-11-02T15:03:44-04:00 — #3
Aha, thanks - I'll read through and digest
I am using PHP, so I could use either. Is one method preferable to use than another?
guelphdad — 2010-11-03T15:37:52-04:00 — #4
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.