Hướng dẫn last insert id mysql

Get ID of The Last Inserted Record

If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately.

In the table "MyGuests", the "id" column is an AUTO_INCREMENT field:

CREATE TABLE MyGuests [
id INT[6] UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR[30] NOT NULL,
lastname VARCHAR[30] NOT NULL,
email VARCHAR[50],
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
]

The following examples are equal to the examples from the previous page [PHP Insert Data Into MySQL], except that we have added one single line of code to retrieve the ID of the last inserted record. We also echo the last inserted ID:

Example [MySQLi Object-oriented]

Chủ Đề