Content · Programming · PHP

PHP mysqli_insert_id() Function: Usage and Examples

Reports the auto-generated ID from the previous INSERT on this connection. Zero may mean no generated ID; this does not replace fetching the inserted record.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

Reports the auto-generated ID from the previous INSERT on this connection.

The examples focus on the operation itself, including the important distinction: PDO::lastInsertId is the PDO equivalent.

What It Does


Use the examples below to observe the behavior on a small dataset before extending it to application code.

Syntax and Return Value


mysqli_insert_id(...) — requires the corresponding optional extension

The displayed signature describes the API or identifies any correction or optional dependency. When a parameter is optional, supply it only when the task requires it.

Example 1: Basic Use


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. A unique constraint on users.email should protect duplicates.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$email = '[email protected]';
$stmt = mysqli_prepare($link, "INSERT INTO users(email, active) VALUES (?, 1)");
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
echo mysqli_insert_id($link);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 2: Checking Behavior


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. A unique constraint on users.email should protect duplicates.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$email = '[email protected]';
$stmt = mysqli_prepare($link, "INSERT INTO users(email, active) VALUES (?, 1)");
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
echo mysqli_insert_id($link);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Example 3: Practical Example


Examples assume a demo database containing users(id, email, active), products(id, name, price), and orders(id, user_id, total) as required by the query. A unique constraint on users.email should protect duplicates.

<?php
$link = mysqli_connect(getenv('DB_HOST') ?: '127.0.0.1', getenv('DB_USER') ?: 'app', getenv('DB_PASSWORD') ?: '', getenv('DB_NAME') ?: 'demo');
mysqli_set_charset($link, 'utf8mb4');
$email = '[email protected]';
$stmt = mysqli_prepare($link, "INSERT INTO users(email, active) VALUES (?, 1)");
mysqli_stmt_bind_param($stmt, "s", $email);
mysqli_stmt_execute($stmt);
echo mysqli_insert_id($link);

This example is meant for a configured application environment; output depends on the installed extension, external service, or database contents.

Important Behavior and Edge Cases


Zero may mean no generated ID; this does not replace fetching the inserted record.

This API is supplied by an optional extension that is not installed in the local test runtime, or requires a different PHP version. The example must be verified in a matching environment.

Comparison with Related Approaches


ApproachDifference
mysqli_insert_idReports the auto-generated ID from the previous INSERT on this connection.
Related API or alternativePDO::lastInsertId is the PDO equivalent.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the operation-specific limitsZero may mean no generated ID; this does not replace fetching the inserted record.
Confusing it with a related APIPDO::lastInsertId is the PDO equivalent.

Key Takeaways


  • Reports the auto-generated ID from the previous INSERT on this connection.
  • Zero may mean no generated ID; this does not replace fetching the inserted record.
  • PDO::lastInsertId is the PDO equivalent.

Use mysqli_insert_id when its documented behavior matches the problem. Test the relevant edge cases in your own runtime before deploying it.

Share this resourceSend this page to someone who may find it useful.