Page 1 of 1

Best way to get ID

Posted: Thu Feb 21, 2008 10:55 am
by cczernia
I'm writing a program the INSERTS some data into an sql table(1). The table(1) has an ID that is key and autoincrements. I then want to INSERT some more data into another table(2) that has the same ID as table(1).

I'm not really sure how to do this. I could run a SELECT query but I normally use the ID and at that point I don't know what the ID is. How do I find out what the ID is for a record I've just inserted?

Thanks in advance.

Re: Best way to get ID

Posted: Thu Feb 21, 2008 11:05 am
by Zoxive
There are built in functions for this.

Code: Select all

mysql_insert_id(); //mysql
 
$mysqli->insert_id; //mysqli
// or
mysqli_insert_id($Link);

Re: Best way to get ID

Posted: Thu Feb 21, 2008 1:32 pm
by kryles
http://ca3.php.net/mysql

lists mysql functions and short description