mysqli_stmt::$insert_id

mysqli_stmt_insert_id

(PHP 5, PHP 7, PHP 8)

mysqli_stmt::$insert_id -- mysqli_stmt_insert_id — Retourne la valeur gĂ©nĂ©rĂ©e pour une colonne AUTO_INCREMENT par la derniĂšre requĂȘte exĂ©cutĂ©e

Description

Style orienté objet

Style procédural

function mysqli_stmt_insert_id(mysqli_stmt $statement): int|string

Retourne l'ID gĂ©nĂ©rĂ© par une requĂȘte INSERT ou UPDATE sur une table avec une colonne ayant l'attribut AUTO_INCREMENT. Dans le cas d'une requĂȘte INSERT multilignes, elle retourne la premiĂšre valeur gĂ©nĂ©rĂ©e automatiquement qui a Ă©tĂ© insĂ©rĂ©e avec succĂšs. Si LAST_INSERT_ID(expr) est utilisĂ© pour dĂ©finir la valeur de AUTO_INCREMENT, ou dans une requĂȘte UPDATE, la valeur de expr est retournĂ©e Ă  la place.

Retourne 0 si la requĂȘte n'a pas modifiĂ© de valeur AUTO_INCREMENT.

Liste de paramĂštres

statement

Style procédural uniquement : Un objet mysqli_stmt retourné par la fonction mysqli_stmt_init().

Valeurs de retour

La valeur du champ AUTO_INCREMENT modifiĂ© par la requĂȘte. Retourne zĂ©ro si la requĂȘte n'a pas encore Ă©tĂ© exĂ©cutĂ©e ou si elle n'a pas modifiĂ© de valeur AUTO_INCREMENT.

La valeur est propre Ă  l'objet de requĂȘte. Elle n'est pas affectĂ©e par les autres requĂȘtes exĂ©cutĂ©es sur la mĂȘme connexion aprĂšs cette requĂȘte, ni par les requĂȘtes Ă©mises via d'autres connexions ou clients.

Note:

Si le nombre est plus grand que la valeur maximale d'un entier, il sera retourné sous forme de chaßne de caractÚres.

Exemples

Exemple #1 Exemple avec mysqli_stmt::$insert_id

Style orienté objet

<?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("Le nouvel enregistrement a l'ID %d.\n", $stmt->insert_id);

Style procédural

<?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("Le nouvel enregistrement a l'ID %d.\n", mysqli_stmt_insert_id($stmt));

Les exemples ci-dessus vont afficher :

Le nouvel enregistrement a l'ID 1.

Voir aussi

  • mysqli_insert_id() - Retourne la valeur gĂ©nĂ©rĂ©e pour une colonne AUTO_INCREMENT par la derniĂšre requĂȘte
  • mysqli_stmt_affected_rows() - Retourne le nombre total de lignes modifiĂ©es, effacĂ©es, insĂ©rĂ©es, ou correspondant par la derniĂšre requĂȘte
  • mysqli_stmt_execute() - ExĂ©cute une requĂȘte prĂ©parĂ©e
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.