mysql return id after insert – 2 ways to insert and return id mysql

mysql return id after insert : Get ID of The Last Inserted Record. The best WAY to get the id is by using mysql_insert_id() or the mysql SQL function LAST_INSERT_ID().

mysql return id after insert

using LAST_INSERT_ID() function

You need to use the LAST_INSERT_ID() function:

INSERT INTO table_name (col1, col2,...) VALUES ('val1', 'val2'...);

mysql id of inserted row

get id of record created php

$servername = "localhost";
$username = "root";
$password = "[email protected]";
$dbname = "pakainfo_v1";

// Make connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());

$sql = "INSERT INTO Members (firstname, lastname, email_address)
VALUES ('Ravi', 'Rak', '[email protected]')";

if (mysqli_query($conn, $sql)) {
    $last_id = mysqli_insert_id($conn);
    echo "New Members created successfully. your Last Members inserted ID is: " . $last_id;
} else {
    echo "some Error: " . $sql . "<br>" . mysqli_error($conn);


Don’t Miss : Laravel MySQL Get Last Inserted ID

Also Read This ๐Ÿ‘‰   How to String Replace in PHP Functions?

mysql get last inserted id

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

-- using a command in C# use: --
int lastId = (Int32)yourCommand.LastInsertedId;

I hope you get an idea about mysql return id after insert.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, donโ€™t forget to share.