PHP.nl

mysql_insert_id

mysql_insert_id

Get the ID generated in the last query

Waarschuwing: > mysqli_insert_id``PDO::lastInsertId

int **mysql_insert_id** resource $link_identifier

Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).

The ID generated for an AUTO_INCREMENT column by the previous query on success, if the previous query does not generate an AUTO_INCREMENT value, or false if no MySQL connection was established. 0

Voorbeeld: example

<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

Let op: > will convert the return type of the native MySQL C API function to a type of (named in PHP). If your AUTO_INCREMENT column has a column type of BIGINT (64 bits) the conversion may result in an incorrect value. Instead, use the internal MySQL SQL function LAST_INSERT_ID() in an SQL query. For more information about PHP's maximum integer values, please see the documentation. mysql_insert_id``mysql_insert_id()``long``intinteger

Opmerking: > Because acts on the last performed query, be sure to call immediately after the query that generates the value. mysql_insert_id``mysql_insert_id

Opmerking: > The value of the MySQL SQL function always contains the most recently generated AUTO_INCREMENT value, and is not reset between queries. LAST_INSERT_ID()

mysql_query``mysql_info