[thelist] mysql/php: get auto-increment ID for record

Aleem Bawany aleem.bawany at utoronto.ca
Thu Feb 27 03:17:01 CST 2003

I am inserting a record into a mysql DB and leaving
the ID field blank since it's assigned automatically
and is auto-increment. However, I need to use this
ID in a subsequent query to insert into another
table. Is there anyway to get the ID that MySQL
picks for the first query and use that in the second

After some googling, this is what I am down to currently
(some psuedo code):

// insert into first table

// get the last ID from above query:
$query="SELECT * FROM firstTable ORDER BY id DESC LIMIT 1";

// this alternative, is slower I assume:
// $query = "SELECT MAX(id) FROM firstTable";

// insert into secondTable values ("a","b",$ID)

Am I likely to run into trouble/conflicts?
(I think I am)

Is there a better/standard way of doing this?


[ http://aleembawany.com/ ]

More information about the thelist mailing list