开发者

Getting an auto-incremented value from a MySQL database

开发者 https://www.devze.com 2023-02-21 16:10 出处:网络
I am writing a PHP script where data is inserted into a table using a query l开发者_JAVA百科ike \"INSERT INTO y (x, x, x) VALUES (z, z, z). I have a unique, primary, auto-incrementing key that is in t

I am writing a PHP script where data is inserted into a table using a query l开发者_JAVA百科ike "INSERT INTO y (x, x, x) VALUES (z, z, z). I have a unique, primary, auto-incrementing key that is in the table. How can I fetch that number right after the data is inserted and the key is updated with the new row?


mysql_insert_id

more information and example of use: http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html


Depends on how you are making the queries, if you are using some DB abstraction layer or not...

mysql functions: mysql_insert_id

mysqli: mysqli->insert_id

PDO: PDO::lastInsertId


mysql_insert_id — Get the ID generated in the last query

0

精彩评论

暂无评论...
验证码 换一张
取 消