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 — Retourne l'identifiant automatiquement généré par la dernière requête
Description
Style orienté objet
Style procédural
La fonction mysqli_insert_id() retourne l'identifiant généré par une requête sur une table avec une colonne possédant l'attribut AUTO_INCREMENT. Si la dernière requête n'était ni un INSERT, ni un UPDATE ou que la table modifiée ne possède pas de colonne avec l'attribut AUTO_INCREMENT, cette fonction retournera zéro.
Note:
Exécuter une commande INSERT ou UPDATE utilisant la fonction LAST_INSERT_ID() modifiera aussi la valeur retournée par la fonction mysqli_insert_id().
Liste de paramètres
-
link -
Seulement en style procédural : Un identifiant de lien retourné par la fonction mysqli_connect() ou par la fonction mysqli_init()
Valeurs de retour
La valeur du champ AUTO_INCREMENT modifiée par la dernière requête. Retourne zéro s'il n'y a pas eu de requête sur la connexion ou si la dernière requête n'a pas modifié la valeur de l'AUTO_INCREMENT.
Note:
Si le nombre est plus grand que la valeur maximale d'un entier, mysqli_insert_id() retournera une chaîne le représentant.
Exemples
Exemple #1 Exemple avec $mysqli->insert_id
Style orienté objet
<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* Vérification de la connexion */
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %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 ("Le nouvel enregistrement a l'id %d.\n", $mysqli->insert_id);
/* drop table */
$mysqli->query("DROP TABLE myCity");
/* Fermeture de la connexion */
$mysqli->close();
?>
Style procédural
<?php
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* Vérification de la connexion */
if (mysqli_connect_errno()) {
printf("Échec de la connexion : %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 ("Le nouvel enregistrement a l'id %d.\n", mysqli_insert_id($link));
/* drop table */
mysqli_query($link, "DROP TABLE myCity");
/* Fermeture de la connexion */
mysqli_close($link);
?>
Les exemples ci-dessus vont afficher :
Le nouvel enregistrement a l'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.
