English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to use PHP to retrieve the unique ID of the last inserted row from a MySQL database table.
In " PHP MySQL InsertIn this chapter, you have learned that AUTO_INCREMENT automatically generates a unique ID for the column each time a new record or row is inserted into the table. However, in some cases, you may need to insert the automatically generated ID into a second table. In such cases, you can use the PHP mysqli_insert_id() function to retrieve the most recently generated ID, as shown in the following example.
In this example, we will use the one inPHP MySQL Create Tablethe same chapter created inindividualtable, which has four columnsid,first_name,last_nameandemail, among whichidis the primary key column and marked with the AUTO_INCREMENT flag.
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without password)*/ $link = mysqli_connect("localhost", "root", "", "demo"); //Check connection if ($link === false) { die("Error: Unable to connect. " . mysqli_connect_error()); } //Attempt to execute insert query $sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('Ron', 'Weasley', '[email protected]')"; if (mysqli_query($link, $sql)) { //Get the last inserted ID $last_id = mysqli_insert_id($link); echo "Record inserted successfully. The last inserted ID is: ". $last_id; } else{ echo "Error: Unable to execute $sql. ". mysqli_error($link); } //Close connection mysqli_close($link); ?>
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without password)*/ $mysqli = new mysqli("localhost", "root", "", "demo"); //Check connection if($mysqli === false){ die("Error: Unable to connect. ". $mysqli->connect_error); } // Attempt to execute insert query $sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('Ron', 'Weasley', '[email protected]')"; if($mysqli->query($sql) === true){ //Get the last inserted ID $last_id = $mysqli->insert_id; echo "Record inserted successfully. The last inserted ID is: ". $last_id; } else{ echo "Error: Unable to execute $sql. ". $mysqli->error; } //Close connection $mysqli->close(); ?>
<?php /* Attempt to connect to MySQL server. Assume you are running MySQL. Server with default settings (user 'root' without password)*/ try{ $pdo = new PDO("mysql:host=localhost;dbname=demo", "root", ""); //Set PDO error mode to exception $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e){ die("Error: Unable to connect. ". $e->getMessage()); } //Attempt to execute insert query try{ $sql = "INSERT INTO persons (first_name, last_name, email) VALUES ('Ron', 'Weasley', '[email protected]')"; $pdo->exec($sql); $last_id = $pdo->lastInsertId(); echo "Record inserted successfully. The last inserted ID is: ". $last_id; } catch(PDOException $e){ die("Error: Unable to execute $sql. ". $e->getMessage()); } //Close connection unset($pdo); ?>