Returns the ID generated by an INSERT or
UPDATE statement on a table with a column having the
AUTO_INCREMENT attribute. In the case of a multiple-row
INSERT statement, it returns the first automatically
generated value that was successfully inserted. If
LAST_INSERT_ID(expr) is used to set the
AUTO_INCREMENT value, or in an UPDATE,
the value of expr is returned instead.
Returns 0 if the statement did not change an
AUTO_INCREMENT value.
The value of the AUTO_INCREMENT field that was updated
by the statement. Returns zero if the statement has not been executed yet or
if it did not update an AUTO_INCREMENT value.
The value is specific to the statement object. It is not affected by
other statements executed on the same connection after this statement,
nor by statements issued using other connections or clients.
Note:
If the number is greater than the maximum int value, it will be returned as
a string.
Examples
Example #1 mysqli_stmt::$insert_id example
Object-oriented style
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");
$stmt = $mysqli->prepare("INSERT INTO myCity (Name, CountryCode, District, Population) VALUES (?, ?, ?, ?)");
$name = 'Stuttgart';
$countryCode = 'DEU';
$district = 'Stuttgart';
$population = 617000;
$stmt->bind_param('sssi', $name, $countryCode, $district, $population);
$stmt->execute();
printf("New record has ID %d.\n", $stmt->insert_id);