mysqli::$insert_id

mysqli_insert_id

(PHP 5, PHP 7, PHP 8)

mysqli::$insert_id -- mysqli_insert_idReturns the value generated for an AUTO_INCREMENT column by the last query

Açıklama

Nesne yönelimli kullanım

Yordamsal kullanım

mysqli_insert_id(mysqli$mysql): int|string

Returns the ID generated by an INSERT or UPDATE query 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.

Performing an INSERT or UPDATE statement using the LAST_INSERT_ID() MySQL function will also modify the value returned by mysqli_insert_id(). If LAST_INSERT_ID(expr) was used to generate the value of AUTO_INCREMENT, it returns the value of the last expr instead of the generated AUTO_INCREMENT value.

Returns 0 if the previous statement did not change an AUTO_INCREMENT value. mysqli_insert_id() must be called immediately after the statement that generated the value.

Bağımsız Değişkenler

bağlantı

Sadece yordamsal tarz: mysqli_connect() veya mysqli_init() işlevinden dönen bir mysqli nesnesi.

Dönen Değerler

The value of the AUTO_INCREMENT field that was updated by the previous query. Returns zero if there was no previous query on the connection or if the query did not update an AUTO_INCREMENT value.

Only statements issued using the current connection affect the return value. The value is not affected 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->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 TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);

printf("New record has ID %d.\n", $mysqli->insert_id);


$mysqli->query("DROP TABLE myCity");

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 TABLE myCity LIKE City");

$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($link, $query);

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


mysqli_query($link, "DROP TABLE myCity");

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

New record has ID 1.
To Top