PHP mysqli_insert_id() Function

PHP mysqli_insert_id() function returns the auto-generated id used in the last query.


mixed mysqli_insert_id(connection);

mysqli_insert_id() Function Parameter

Connection :Required parameter. The MySQL connection to be used

mysqli_insert_id() Function Return Value

Return Values :Returns the value of the AUTO_INCREMENT field that was updated by the previous query.

mysqli_insert_id() Function Example

$con = mysqli_init();

if (!mysqli_real_connect($con,"localhost","user","password","db"))
  die("Connection Error: " . mysqli_connect_error());

$insert_member_sql = "INSERT INTO members (id, username) VALUES('$id', '$username')";
mysqli_query($con, $insert_member_sql);

echo "New Record has id : " . mysqli_insert_id($con);