English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP mysqli_insert_id() Function Usage and Example

PHP MySQLi Reference Manual

The mysqli_insert_id() function returns the auto-increment ID generated by the last inserted statement

Definition and Usage

If you have a table with an AUTO_INCREMENT column and if your last MySQLi function call executes an INSERT or UPDATE statement. The function's purpose is to: return the ID of the last query executed automatically.

Syntax

mysqli_insert_id($con)

Parameter

Serial NumberParameters and Description
1

con (required)

This is an object representing the connection with the MySQL Server.

Return value

The mysqli_insert_id() function returns the value of the column set to AUTO_INCREMENT in the table where the last SQL statement (usually an INSERT statement) was operated. If the last SQL statement is not an INSERT or UPDATE statement, or there is no column set to AUTO_INCREMENT in the table operated, the return value is 0.

PHP version

This function was initially introduced in PHP version5introduced and can be used in all higher versions.

Assuming we have created a table named Cricketers in the database mydb, where the field ID is a PRIMARY KEY and is AUTO INCREMENTED:

CREATE TABLE Cricketers(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   First_Name VARCHAR(255), 
   Last_Name VARCHAR(255), 
   Date_Of_Birth date, 
   Place_Of_Birth VARCHAR(255), 
   Country VARCHAR(255);
);

Online Examples

The following example demonstratesmysqli_insert_id()Usage of the function (procedural style)-

<?php
   //Establishing connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Inserting a record into the employee table
   $sql = \1, \1981-12-05), \
   mysqli_query($con, ";");
   //Insert ID
   $id = mysqli_insert_id($con);
   print("Insert ID: "+$id+\n);
   $sql = \2, \1981-04-22), \
   mysqli_query($con, ";");
   $id = mysqli_insert_id($con);
   print("Insert ID: "+$id);
   //Close Connection
   mysqli_close($con);
?>

Output Result

Insert ID: 1
Insert ID: 2

Online Examples

In an object-oriented style, the syntax of this function is$ con-> insert_id();。Here is an example of the object-oriented style $ minus's This function;

<?php
   //Establishing connection
   $con = new mysqli("localhost", "root", "password", "mydb");
   //Inserting a record into the employee table
   $con -> query("insert into Cricketers values(3, 'Kumara', 'Sangakkara', DATE('1977-10-27), 'Matale', 'Srilanka')");
   //Insert ID
   $state = $con-> insert_id;
   print("Insert ID: ".$state."\n");
   //Inserting a record into the employee table
   $con -> query("insert into Cricketers values(4, 'Virat', 'Kohli', DATE('1988-11-05), 'Delhi', 'India')");
   //Insert ID
   $state = $con-> insert_id;
   print("Insert ID: ".$state);
   //Close Connection
   $con -> close();
?>

Output Result

Insert ID: 3
Insert ID: 4

Online Examples

Here is the functionmysqli_insert_id'sAnother example-

<?php
   //Establishing connection
   $con = mysqli_connect("localhost", "root", "password", "mydb");
   //Query to select all rows from the Cricketers table
   mysqli_query($con, "SELECT * FROM Cricketers");
   print("Insert ID (select query): ".mysqli_insert_id($con)."\n");
   //Query used to insert multiple rows into the Cricketers table
   mysqli_query($con, "INSERT INTO Cricketers VALUES (51987-04-306, 'Ravindra', 'Jadeja', DATE('1988-12-06), 'Nagpur', 'India') ");
   print("Insert ID: (multiple inserts) ".mysqli_insert_id($con)."\n");
   //Query to update the rows in the employee table
   mysqli_query($con, "UPDATE Cricketers set COUNTRY = 'S.Africa' where ID = ") 2"
   print("Insert ID (update query): " . mysqli_insert_id($con) . "\n");
   //The query inserts a record into the employee table
   mysqli_query($con, "INSERT INTO employee VALUES ('Sarmista', 'Sharma', 28, 'F', 15000,  101);
   print("Insert ID: (table without auto incremented key) " . mysqli_insert_id($con) . "\n");
   //Close Connection
   mysqli_close($con);
?>

Output Result

Insert ID (select query): 0
Insert ID: (multiple inserts) 6
Insert ID (update query): 0
Insert ID: (table without auto incremented key) 0

Online Examples

The following examples demonstrate SELECT, UPDATE, INSERT, and DELETE queries inmysqli_insert_idUsage of Function-

<?php
   $connection_mysql = mysqli_connect("localhost", "root", "password", "mydb");
   
   if (mysqli_connect_errno($connection_mysql)){
      echo "Connection to MySql failed: " . mysqli_connect_error();
   }
   
   mysqli_query($connection_mysql, "INSERT INTO Employee (name) VALUES('PHP')");
   echo "New record has id: " . mysqli_insert_id($connection_mysql); 
   
   mysqli_close($connection_mysql);
?>

Output Result

New record has id: 0

PHP MySQLi Reference Manual