AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi Get Last ID


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

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 Email
1 Clark Kent clarkkent [email protected]
2 John Carter johncarter [email protected]
3 Peter Parker peterparker [email protected]

 

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.

Example (this is HTML - PHP editor, change text on this window)

<?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', '[email protected]')";


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)

Example (this is HTML - PHP editor, change text on this window)

<?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', '[email protected]')";

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

Example (this is HTML - PHP editor, change text on this window)

<?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', '[email protected]'";
// 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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


jQuery replace...
jQuery replaceClass...
jQuery click ()...
jQuery switchClass()...
jQuery draggable()...
jQuery HTML...
jQuery scroll smooth...
jQuery scroll()...
jQuery stop()...
jQuery animate()...
jQuery SlideToggle()...
jQuery slideDown()...
jQuery slideUp()...
jQuery fadeToggle...
jQuery fadeOut()...
jQuery fadeIn()...
jQuery Effects - hide () and show ()...
jQuery Events...
jQuery Attributes...
jQuery Selectors...