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 — Retorna o id gerado automaticamente na última consulta
Descrição
Modo procedural:
Modo orientado a objeto (propriedade):
A função mysqli_insert_id() retorna o ID gerado pela consulta em uma tabela com uma coluna com o atributo AUTO_INCREMENT. Se a última consulta não foi um INSERT ou UPDATE ou se a tabela modificada não tem uma coluna com o atributo AUTO_INCREMENT, esta função irá retornar zero.
Nota:
Executar uma consulta INSERT ou UPDATE usando a função LAST_INSERT_ID() irá também modificar o valor retornado pela função mysqli_insert_id().
Parâmetros
-
link -
Procedural style only: A link identifier returned by mysqli_connect() or mysqli_init()
Valor Retornado
O valor do campo AUTO_INCREMENT que foi atualizado pela consulta anterior. Retorna zero se não houve uma consulta anterior na conexão ou se a consulta não atualizou um valor AUTO_INCREMENT.
Nota:
Se o número for maior do que o maior valor inteiro possível, mysqli_insert_id() irá retornar uma string.
Exemplos
Exemplo #1 Estilo orientado a objeto
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %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 ("New Record has id %d.\n", $mysqli->insert_id);
/* drop table */
$mysqli->query("DROP TABLE myCity");
/* close connection */
$mysqli->close();
?>
Exemplo #2 Estilo de procedimento
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %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 ("New Record has id %d.\n", mysqli_insert_id($link));
/* drop table */
mysqli_query($link, "DROP TABLE myCity");
/* close connection */
mysqli_close($link);
?>
O exemplo acima irá imprimir:
New Record has 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.
