Easy Tutorial
❮ Pdostatement Columncount Func Array Diff Key ❯

PHP mysqli_insert_id() Function

PHP MySQLi Reference Manual

Assuming the websites table has an automatically generated ID field. Return the ID from the last query:

<?php 
// Assume database username: root, password: 123456, database: tutorialpro 
$con = mysqli_connect("localhost", "root", "123456", "tutorialpro"); 
if (mysqli_connect_errno($con)) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

mysqli_query($con, "INSERT INTO websites (name, url, alexa) 
VALUES ('tutorialpro.org', 'http://www.tutorialpro.org', 5633)");

// Output the automatically generated ID
echo "New ID is: " . mysqli_insert_id($con); 

mysqli_close($con);
?>

Definition and Usage

The mysqli_insert_id() function returns the ID generated by the last query (created through AUTO_INCREMENT).


Syntax

Parameter Description
connection Required. Specifies the MySQL connection to use.

Technical Details

Return Value: Returns an integer representing the value of the AUTO_INCREMENT field generated in the last query. If the number exceeds the maximum integer value, it returns a string. Returns 0 if there are no updates or no AUTO_INCREMENT field.
PHP Version: 5+
--- ---
❮ Pdostatement Columncount Func Array Diff Key ❯