PHP MySQLi Get Last ID
PHP Tutorial » PHP MySQLi Get Last ID
How can I get the last insert / updated ID in PHP?
We have various methods for selecting the last insertion id from the MySQL table.
1. We can select a single row from the table in descending order and store the id.
2. Select Maximum value.
If we INSERT or UPDATE a row on a SQL table with AUTO_INCREMENT field, we can get the ID of the last inserted or updated record from table.
We have a table "Users" like the one below and we insert another row in it, then we get the last ID from DataBase.ID | First Name | Last Name | UserName | |
---|---|---|---|---|
1 | Clark | Kent | clarkkent | clarkkent@mail.com |
2 | John | Carter | johncarter | johncarter@mail.com |
3 | Peter | Parker | peterparker | peterparker@mail.com |
PHP Get Last ID - MySQLi (object oriented)
Before accessing data from the MySQL database, we must be able to connect to the server.
For this example we'll use the same Users table that we've created in the PHP MySQL create tables, which has three rows and five columns as: id, first_name, last_name, user_name and email, where id is the primary key column and marked with AUTO_INCREMENT flag.
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}
$sql = "INSERT INTO Users (first_name, last_name, user_name, email)
VALUES ('Stwart', 'Martin', 'StwartM', 'StwartM@agernic.com')";
if ($link->query($sql) === TRUE) {
$last_id = $link->insert_id;
echo "New record created successfully. Last inserted ID is: " . $last_id;;
} else {
echo "Error: " . $sql . "<br />" . $link->error;
}
$link->close();
?>
Related subjects:
How to create database phpmyadmin?
Insert multiple rows
PHP select / view data
Conect to MySQLi
Tags:
How do I get the last inserted ID of a MySQL table in PHP?
How do I get the last row inserted id in MySQL?
How can I get last insert ID in PDO?
How can I get the last updated ID in PHP?
How do I find the last ID in MySQL?
How can I get my last ID?
PHP MySQLi last: insert id, error, last_insert_id, id, query, update id, row, get last insert id, get last query, php mysqli_query last insert id
PHP MySQLi get: last insert id, data, all rows, first row, get error, number of rows, single row, single value, field namess
PHP Get Last ID - MySQLi (Procedural)
How do I get the last row inserted id in MySQL? Example using MySQLi (Procedural)
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO Members (first_name, last_name, user_name, email)
VALUES ('Stwart', 'Martin', 'StwartM', 'StwartM@agernic.com')";
if (mysqli_query($link, $sql)) {
$last_id = mysqli_insert_id($link);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br />" . mysqli_error($link);
}mysqli_close($link);
?>
PHP Get Last ID - MySQLi (PDO) extension
The following example insert another row and get the last ID from table named "Users" in database my_DB using PDO
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
try {
$link = new PDO("mysql:host=$server_name;dbname=$db_name",
$user_name, $password);
// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO Members (first_name, last_name, user_name, email)
VALUES ('Stwart', 'Martin', 'StwartM', 'StwartM@agernic.com'";
// use exec() because no results are returned
$link->exec($sql);
$last_id = $conn->lastInsertId();
echo "New record created successfully. Last inserted ID is: " . $last_id;
} catch(PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
$link = null;
?>
Output
last id : 4
How do I find the last ID in MySQL?
How can I get my last ID?
PHP MySQLi last: insert id, error, last_insert_id, id, query, update id, row, get last insert id, get last query, php mysqli_query last insert id
PHP MySQLi get: last insert id, data, all rows, first row, get error, number of rows, single row, single value, field namess
PHP MySQLi Get Last ID - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.