2011-04-08 27 views
0

可能重複:
Is there a way to return the id of a row that was just created in MySQL with PHP?如何在PHP中獲得SQL inerst命令後數據

您好,我有一個嵌入的命令,比如

insert (name) values($name); 

在那裏我有id列作爲outtocrement如何直接插入後如何獲得插入記錄的ID

+2

重複的http://stackoverflow.com/questions/669942/is-there-a-way-to-return-the-id-of-a-row-that-was-just-created-在MySQL的與 - PHP – Albireo 2011-04-08 17:40:45

回答

2

如果您正在使用mysql_*功能的工作,你將不得不使用mysql_insert_id()(引用)

由 前面的查詢(通常INSERT)獲取的 AUTO_INCREMENT列生成的ID。


隨着mysqli的,你會使用mysqli::insert_id()

而且,通過PDO,您可以撥打PDO::lastInsertId()

0
mysql_query("Your query here"); 
$last_id = mysql_insert_id();