简体   繁体   中英

How can i get a auto generated primary key id from a table after completion of insertion query?

I have to insert in two table. but for insertion in second table i need the latest id of the first table after insertion in first table. since second table is to many to many relation of first table and another table. table1(id, name, age) and table2(id, street, table1_id). 1st i will insert into table1 then the id of the inserted row will insert into table2 as table1_id. how i can get the id of last inserted row of table1.

You can simple use mysql_insert_id(); just after your insert query like this:

$sql_save12 = mysql_query("insert into table(classId,pay_mode_id,) values ('44','2',)");

$insert = mysql_insert_id();

$insert will be your new inserted id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM