PHP mysqli_insert_id() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
MySQLi

Definition

In PHP, mysqli_insert_id() returns the AUTO_INCREMENT value generated by the last INSERT (or UPDATE) on the connection, or 0 when no id was generated.

Call it on the same connection right after the statement that created the row. On very large ids PHP may return a string instead of an int.

Syntax

Call mysqli_insert_id() with the values you need. Start with this line.

PHP
$id = mysqli_insert_id($mysqli);
CallWhat it does
mysqli_insert_id($mysqli)Returns the last AUTO_INCREMENT id for this connection.
$id = mysqli_insert_id($mysqli);Stores the new primary key for a follow-up INSERT.
echo mysqli_insert_id($mysqli)Prints the generated id after a successful insert.

The value is per connection, not global across the whole server. Another PHP request using a different connection will not see your id. If the INSERT failed, do not trust insert_id until you check the query result.

Library declarations
PHP
mysqli_insert_id(mysqli $mysql): int|string

Parameters

ParameterDescription
mysqlRequired. The mysqli connection that ran the INSERT.

Return Value

Return ValueDescription
int|stringThe generated AUTO_INCREMENT value, or 0 if none was produced. Very large values may be returned as a string.

PHP Examples

Example 1 — Get the Id After Insert

Insert one row and print the new AUTO_INCREMENT value. Sample id is fixed.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

mysqli_query(
    $mysqli,
    "INSERT INTO users (name, email) VALUES ('Ada', '[email protected]')"
);

echo "New id: ", mysqli_insert_id($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. Run INSERT on a table with an AUTO_INCREMENT primary key.
  2. Immediately call mysqli_insert_id() on the same link.
  3. The sample returns 42 as the new id.
  4. Use that id for related rows in other tables.

Example 2 — Zero When No AUTO_INCREMENT Fired

A statement that does not generate an id returns 0.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

mysqli_query($mysqli, "UPDATE users SET name = 'Ada' WHERE id = 1");
echo mysqli_insert_id($mysqli), PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. This UPDATE does not create an AUTO_INCREMENT value.
  2. mysqli_insert_id() therefore returns 0.
  3. 0 means "no id generated", not necessarily an error.
  4. Check the query return value separately for success or failure.

Example 3 — Use Insert Id in a Second Query

Create a parent row, then insert a child row that references the new id.

PHP
<?php
$mysqli = mysqli_connect("localhost", "demo_user", "demo_pass", "app_db");

mysqli_query($mysqli, "INSERT INTO orders (customer) VALUES ('Ada')");
$orderId = mysqli_insert_id($mysqli);

mysqli_query(
    $mysqli,
    "INSERT INTO order_items (order_id, sku) VALUES ($orderId, 'BOOK-1')"
);

echo "Order ", $orderId, " saved.", PHP_EOL;

mysqli_close($mysqli);

How It Works

  1. First INSERT creates the parent order row.
  2. mysqli_insert_id() captures the new order id.
  3. The second INSERT stores a line item that points at that id.
  4. In real apps prefer prepared statements for the values.

Frequently Asked Questions

The AUTO_INCREMENT value from the last INSERT/UPDATE on that connection, or 0 if none was generated.
The last statement did not create an AUTO_INCREMENT id, the INSERT failed, or you queried a different connection object.
Yes. It returns the first generated id in the batch.
Usually yes, but very large ids may come back as a string. Compare carefully.
No. The value is tracked per connection. Other scripts have their own connections.

Next: mysqli_kill()

mysqli_kill() asks the MySQL server to terminate a thread (connection) identified by a pro.

mysqli_kill() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful