PHP mysqli_insert_id() function returns the auto-generated id used in the last query.
Syntax
mixed mysqli_insert_id(connection);
Parameter | Description |
---|---|
Connection : | Required parameter. The MySQL connection to be used |
Return Values : | Returns the value of the AUTO_INCREMENT field that was updated by the previous query. |
<?php $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); mysqli_close($con); ?>