mysql get last insert id code example

Example 1: mysql id of inserted row

-- For mysql
INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...);
SELECT LAST_INSERT_ID();

Example 2: mysql set last_insert_id

SET @last_id = LAST_INSERT_ID();

Example 3: mysql get last inserted id

-- To get the last inserted auto-increment row ID: --
SELECT LAST_INSERT_ID( optional_expression )

-- If you have just inserted it using a command in C# use: --
int lastId = (Int32)yourCommand.LastInsertedId;

Example 4: find last id

//laravel controller
$newOrder = new Order();
...
...
$newOrder->save();

$last_id = $newOrder->id;

Tags:

Php Example