I have received many statements that the insert_id property has a bug because it "works sometimes". Keep in mind that when using the OOP approach, the actual instantiation of the mysqli class will hold the insert_id.
The following code will return nothing.
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$result->insert_id;
}
?>
This is because the insert_id property doesn't belong to the result, but rather the actual mysqli class. This would work:
<?php
$mysqli = new mysqli('host','user','pass','db');
if ($result = $mysqli->query("INSERT INTO t (field) VALUES ('value');")) {
echo 'The ID is: '.$mysqli->insert_id;
}
?>
mysqli::$insert_id
mysqli_insert_id
(PHP 5)
mysqli::$insert_id -- mysqli_insert_id — Возвращает автоматически генерируемый ID, используя последний запрос
Описание
Объектно-ориентированный стиль
Процедурный стиль
Функция mysqli_insert_id() возвращает ID, генерируемый запросом к таблице, которая содержит колонку с атрибутом AUTO_INCREMENT. Если последний запрос не был INSERT или UPDATE или в модифицируемой таблице отсутствует колонка с атрибутом AUTO_INCREMENT, данная функция вернет ноль.
Замечание:
Выполнение выражения INSERT или UPDATE, используя функцию LAST_INSERT_ID(), также изменяет значение, возвращаемое функцией mysqli_insert_id().
Список параметров
-
link -
Только для процедурного стиля: Идентификатор соединения, полученный с помощью mysqli_connect() или mysqli_init()
Возвращаемые значения
Значение поля AUTO_INCREMENT, которое было затронуто предыдущим запросом. Возвращает ноль, если предыдущий запрос не затронул таблицы, сожержащие поле AUTO_INCREMENT.
Замечание:
Если число больше максимального значения int, функция mysqli_insert_id() вернет строку.
Примеры
Пример #1 Пример функции $mysqli->insert_id
Объектно-ориентированный стиль
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* проверка соединения */
if (mysqli_connect_errno()) {
printf("Соединение не установлено: %s\n", mysqli_connect_error());
exit();
}
$mysqli->query("CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
$mysqli->query($query);
printf ("ID новой записи: %d.\n", $mysqli->insert_id);
/* удаление таблицы */
$mysqli->query("DROP TABLE myCity");
/* закрытие соединения */
$mysqli->close();
?>
Процедурный стиль
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* проверка соединения */
if (mysqli_connect_errno()) {
printf("Соединение не установлено: %s\n", mysqli_connect_error());
exit();
}
mysqli_query($link, "CREATE TABLE myCity LIKE City");
$query = "INSERT INTO myCity VALUES (NULL, 'Stuttgart', 'DEU', 'Stuttgart', 617000)";
mysqli_query($link, $query);
printf ("ID новой записи: %d.\n", mysqli_insert_id($link));
/* удаление таблицы */
mysqli_query($link, "DROP TABLE myCity");
/* закрытие соединения */
mysqli_close($link);
?>
Результат выполнения данных примеров:
ID новой записи: 1.
When running extended inserts on a table with an AUTO_INCREMENT field, the value of mysqli_insert_id() will equal the value of the *first* row inserted, not the last, as you might expect.
<?
//mytable has an auto_increment field
$db->query("INSERT INTO mytable (field1,field2,field3) VALUES ('val1','val2','val3'),
('val1','val2','val3'),
('val1','val2','val3')");
echo $db->insert_id; //will echo the id of the FIRST row inserted
?>
Watch out for the oo-style use of $db->insert_id. When the insert_id exceeds 2^31 (2147483648) fetching the insert id renders a wrong, too large number. You better use the procedural mysqli_insert_id( $db ) instead.
[EDIT by danbrown AT php DOT net: This is another prime example of the limits of 32-bit signed integers.]
I was having problems with getting the inserted id, and did a bit of testing. It ended up that if you commit a transaction before getting the last inserted id, it returns 0 every time, but if you get the last inserted id before committing the transaction, you get the correct value.
Some people are wondering how to get the ids of the rows inserted with loop. Here is it:
<?php
// stmt_init ...
for (;;;) {
// do some things...
$stmt->execute();
$ids[] = $mysqli->insert_id;
}
?>
Note that if you Call a MySQL stored procedure to insert a new record and then reference $db->insert_id; you will get 0 back, not the last inserted ID.
It is therefore necessary to add a line to your MySQL Stored Procedure such as
select last_insert_id() as intRecordKey;
after the insert so that the query will return the new key value.
Then in your php code you can use the following
<?php
$objInsertResult = $db->query($strSQL);
$objInsertRow = $objInsertResult->fetch_object();
$intRecordKey = $objInsertRow->intRecordKey;
?>
It would however be nice if $db->insert_id did return the last insert ID following a stored procedure call.
