Current Location: Home> Latest Articles> PHP Tutorial: How to Use mysqli_insert_id to Get Auto-Increment Primary Key in Database

PHP Tutorial: How to Use mysqli_insert_id to Get Auto-Increment Primary Key in Database

M66 2025-07-16

PHP Database Insert Operation: How to Use mysqli_insert_id to Get Auto-Increment Primary Key

In PHP development, interacting with databases is a very common task. Among these operations, inserting data is fundamental and frequently used. Often, tables have an auto-increment primary key to uniquely identify each record, and knowing how to retrieve the auto-increment key value is essential for developers.

This article will explain how to easily get the auto-increment primary key of the newly inserted record using the mysqli_insert_id function, helping you better manage your database operations.

Establish Database Connection

Before inserting data, you need to establish a database connection. Use the mysqli_connect function to connect to the database server. Once connected successfully, you can proceed with database operations.

<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

// Create database connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check if connection was successful
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
?>

In this code, please replace $servername, $username, $password, and $dbname with your actual database credentials.

Execute Insert Operation

After connecting to the database successfully, you can perform the insert operation. Ensure your table has an auto-increment primary key field so you can retrieve the key value using mysqli_insert_id.

<?php
// SQL statement to insert data
$sql = "INSERT INTO users (username, email) VALUES ('John Doe', 'johndoe@example.com')";

// Check if the insert was successful
if (mysqli_query($conn, $sql)) {
    $lastInsertedId = mysqli_insert_id($conn);
    echo "New record inserted successfully, auto-increment ID is: " . $lastInsertedId;
} else {
    echo "Insert failed: " . mysqli_error($conn);
}

// Close the database connection
mysqli_close($conn);
?>

This code snippet shows how to insert a record into the users table. After a successful insert, it uses mysqli_insert_id to get the last inserted auto-increment ID for the current connection and outputs it.

Understanding mysqli_insert_id Function

The mysqli_insert_id function returns the ID generated by a previous INSERT query on a table with a column having the AUTO_INCREMENT attribute. It requires the database connection object as a parameter.

Note that this function must be called immediately after the INSERT operation; otherwise, it will not return the correct key value.

Conclusion

This article demonstrated how to use mysqli_insert_id in PHP to retrieve the auto-increment primary key after inserting data into a database. Mastering this technique helps developers manage data relationships more effectively and implement flexible business logic.

Database insert operations are an essential part of PHP development. Correctly obtaining auto-increment keys not only facilitates subsequent processing but also improves the overall stability and scalability of applications.

We hope this guide makes your PHP development smoother. Happy coding!