Posted inProgramming / Mysql / Mysqli / php

PHP MySQLi Get ID of Last Inserted Record

PHP MySQLi Get ID of Last Inserted Record

Today, We want to share with you PHP MySQLi Get ID of Last Inserted Record.
In this post we will show you PHP Get ID of Last Inserted Record Some Example, hear for php – Get ID from last insert into table we will give you demo and example for implement.
In this post, we will learn about Get Last Inserted ID in MySQL Table Using PHP with an example.

Simple insert a record data into in database a table that all contains an AUTO_INCREMENT column uniq id get need to this id onther column add this id.
fetch the last inserted record ID in a MySQL database using PHP

Example 1 – MySQLi Object-oriented
Example 2 – MySQLi Procedural
Example 3 – PDO

Example 1 : how to get last inserted record in MySQLi Object-oriented using php

//insert query using PHP
$sql_query = "INSERT INTO salesorder (itemcode, description, pono)
VALUES ('so-300056', 'boookno1', '9825645895')";

if ($conn->query($sql_query) === TRUE) { //condition check
    $last_id_value = $conn->insert_id; //get last autoincrement id inserted
    echo "simple New record date created successfully inserted. The Last record inserted ID value is: " . $last_id_value;
}
else 
{
	//error condition
    echo "An Error generate : " . $sql_query . "
" . $conn->error; }

Example 2 : how to get last inserted record in MySQLi Procedural using php


//insert query using PHP
$sql_query = "INSERT INTO salesorder (itemcode, description, pono)
VALUES ('so-300056', 'boookno1', '9825645895')";

if (mysqli_query($conn, $sql_query)) 
{
	//get last autoincrement id inserted
    $last_id_value = mysqli_insert_id($conn);
	
	//print this uniq id with message
    echo "simple New record data created successfully inserted. The Last record inserted ID value is: " . $last_id_value;
}
else 
{
	//error condition
    echo "An Error generate : " . $sql_query . "
" . $conn->error; }

Example 3 : Getting the last INSERTed record from PDO


try 
{
	//database connection..
    $conn_qu = new PDO("mysql:host=$hostname;dbname=$databasename", $usernm, $pass);
   
	//insert query using PHP
	$sql_query = "INSERT INTO salesorder (itemcode, description, pono)
	VALUES ('so-300056', 'boookno1', '9825645895')";
   
	//execute query using PHP
    $conn_qu->exec($sql_query);
	
	//get last autoincrement id inserted
    $last_id_value = $conn_qu->lastInsertId();
	
	//print this uniq id 
    echo "simple New record data created successfully inserted. The Last record inserted ID value is: " . $last_id_value;
}
catch(PDOException $e) //errror section
{
	echo $sql_query . "
" . $e->getMessage(); // call function }

I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I'm a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.

Leave a Reply

Your email address will not be published. Required fields are marked *

We accept paid guest Posting on our Site : Guest Post Chat with Us On Skype