Updated PHP Windows builds for Brotli security fix

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

+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.
To Top