mysqli_stmt::$insert_id

mysqli_stmt_insert_id

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$insert_id -- mysqli_stmt_insert_id — Returns the value generated for an AUTO_INCREMENT column by the last statement executed

Açıklama

Nesne yönelimli kullanım

Yordamsal kullanım

function mysqli_stmt_insert_id(mysqli_stmt $statement): int|string

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.

Bağımsız Değişkenler

deyim

Sadece yordamsal tarz: mysqli_stmt_init() işlevinden dönen bir mysqli_stmt nesnesi.

Dönen Değerler

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.

Bilginize:

If the number is greater than the maximum int value, it will be returned as a string.

Örnekler

Örnek 1 mysqli_stmt::$insert_id example

Nesne yönelimli kullanım

<?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);

Yordamsal kullanım

<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");

$stmt = mysqli_prepare($link, "INSERT INTO myCity (Name, CountryCode, District, Population) VALUES (?, ?, ?, ?)");

$name = 'Stuttgart';
$countryCode = 'DEU';
$district = 'Stuttgart';
$population = 617000;
mysqli_stmt_bind_param($stmt, 'sssi', $name, $countryCode, $district, $population);
mysqli_stmt_execute($stmt);

printf("New record has ID %d.\n", mysqli_stmt_insert_id($stmt));

Yukarıdaki örneklerin çıktısı:

New record has ID 1.

Ayrıca Bakınız

+add a note

User Contributed Notes 2 notes

up
7
hgernhardt at tcnewengland dot org
15 years ago
It should be noted that using mysqli_stmt->insert_id will not result in a unique ID being returned for each execution of a prepared insert statement.  In practice, it appears that the first insertion ID is returned.  If you are performing multiple inserts with the same prepared statement (one invocation of mysqli_stmt::prepare and multiple invocations of mysqli_stmt::execute() for a given statement), and need to keep the unique ID for each insert, use mysqli_connection->insert_id.
up
3
Aurelien Marchand
11 years ago
for those with zerofilled IDs beware as mysqli_stmt::$insert_id will return an integer. In other words, if the ID returned from your query was #000099, the value produced by mysqli_stmt::$insert_id is now #99.

I have not found a way to fix this beside using sprintf('%06d',$id) but this assumes you know in advance the size of the ID beforehand.