Tagged: how to get last inserted data in mysql using php

PHP MySQLi Get ID of Last Inserted Record 0

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 . "<hr/>" . $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 . "<hr/>" . $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 . "<br>" . $e->getMessage(); // call function
}

Get last inserted record ID mySQL using PHP 0

Get last inserted record ID mySQL using PHP

Get last inserted record ID mySQL using PHP

Today, We want to share with you Get last inserted record ID mySQL using PHP.
In this post we will show you Get last inserted record ID mySQL using PHP, hear for Get last inserted record ID mySQL using PHP we will give you demo and example for implement.
In this post, we will learn about Get last inserted record ID mySQL using PHP with an example.

If we perform an mysqli using Database INSERT statement or UPDATE statement on a table(mysql or mysqli) with an AUTO_INCREMENT(primary fields) field, we can get the ID(directly get last id) of the last inserted/updated(statement) record immediately.

PHP Get ID of Last Inserted Record

mysqli::$insert_id or mysqli_insert_id — It’s Returns the auto generated(uniq) id used in the latest query in PHP functions

No Function Support Used functions
1 MySQLi Object-oriented $last_id = $conn->insert_id;
2 MySQLi Procedural $last_id = mysqli_insert_id($conn);
3 PDO – Example $last_id = $conn->lastInsertId();
4 Mysql – Example $last_id =mysql_insert_id();

MySQLi Object-oriented – $last_id = $conn->insert_id;

$your_last_id = $conn->insert_id;

 <?php
$sql_query = "INSERT INTO ng4free (postname, categoryname, url)
VALUES ('laravel', 'php', '[email protected]')";

if ($conn->query($sql_query) === TRUE) {
/*
 * RETURN LAST INSERT ID
 * Returns the last record's ID that was inserted into table 'mytable'
 */
    $your_last_id = $conn->insert_id;
    echo "your New record  created same successfully. your Last inserted ID is: " . $your_last_id;
} else {
    echo "Error Generate: " . $sql_query . "<br>" . $conn->error;
}

$conn->close();
?> 

MySQLi Procedural – mysqli_insert_id($conn);

<?php
$sql_query = "INSERT INTO w2way (postname, categoryname, url)
VALUES ('angular', 'js', '[email protected]')";

if ($conn->query($sql_query) === TRUE) {
/*
 * RETURN LAST INSERT ID
 * Returns the last record's ID that was inserted into table 'mytable'
 */
    $your_last_id = mysqli_insert_id($conn);
    echo "your New record  created same successfully. your Last inserted ID is: " . $your_last_id;
} else {
    echo "Error Generate: " . $sql_query . "<br>" . $conn->error;
}

$conn->close();
?> 

MySQLi PDO – $conn->lastInsertId();

<?php
   $sql_pdo ="INSERT INTO w2way (postname, categoryname, url)
VALUES ('angular', 'js', '[email protected]')";
    // use exec() because no results are returned
    $conn->exec($sql_pdo);
	/*
 * RETURN LAST INSERT ID
 * Returns the last record's ID that was inserted into table 'mytable'
 */
    $your_last_id = $conn->lastInsertId();
    echo "your New record  created same successfully. your Last inserted ID is: " . $your_last_id;
    }
	catch(PDOException $e)
    {
    echo $sql_pdo . "<br>" . $e->getMessage();
    }
?>

View Demo

We hope you get an idea about Get last inserted record ID mySQL using PHP
We would like to have feedback on my Information blog .
Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

We hope This Post can help you…….Good Luck!.

PHP Get ID of Last Inserted Record 0

PHP Get ID of Last Inserted Record

PHP Get ID of Last Inserted Record

Welcome to the In Pakainfo.com website! You will Step By Step learn web programming, easy and very fun. This website allmost provides you with a complete web programming tutorial presented in an easy-to-follow manner. Each web programming tutorial has all the practical examples with web programming script and screenshots available.PHP Get ID of Last Inserted Record

With MySQLi Object-oriented: Return last insert ID in mySQL using PHP

$last_id = $conn->insert_id;

With PDO:

$pdo->lastInsertId();

With Mysqli:

$mysqli->insert_id;

MySQLi Object-oriented – Get Last ID (Get ID of The Last Inserted Record)

$sqlQuery = "INSERT INTO products (productname, productdesc, status)
VALUES ('Rolex', 'Rolex watch', '123')";

//get Last Inserted Record ID
if ($conn->query($sqlQuery) === TRUE) {
    $last_id = $conn->insert_id;
    echo "Your New record created successfully. your Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sqlQuery . "<br>" . $conn->error;
}

Get last inserted record ID from mysql using php

$query = "INSERT into products VALUES('', '".$productname."')";
 $data_query_result = mysql_query($query) or die(mysql_error());
 $new_row_id = mysql_insert_id($data_query_result);
 echo "<pre>";
 echo $new_row_id;
 echo "</pre>";
//$new_row_id holds last inserted id print

Getting the last INSERTed record from Mysql

 int mysql_insert_id (resource [link_identifier])  

MySQL LAST_INSERT_ID example
create table

CREATE TABLE products (
    pid INT AUTO_INCREMENT PRIMARY KEY,
    pdescription VARCHAR(250) NOT NULL
)

insert a new row into the products table

INSERT INTO products(pdescription)
VALUES('MySQL last_insert_id');

LAST_INSERT_ID

SELECT LAST_INSERT_ID();

MySQLi Procedural – Get Last ID (Get ID of The Last Inserted Record)

$conn = mysqli_connect($servername, $username, $password, $dbname);

$sqlQuery = "INSERT INTO products (productname, productdesc, status)
VALUES ('Rolex', 'Rolex watch', '123')";

//get Last Inserted Record ID
if (mysqli_query($conn, $sqlQuery)) {
    $last_id = mysqli_insert_id($conn);
    echo "Your New record created successfully. your Last inserted ID is: " . $last_id;
} else {
    echo "Error: " . $sqlQuery . "<br>" . mysqli_error($conn);
}

PDO – Get Last ID (Get ID of The Last Inserted Record)

$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sqlQuery = "INSERT INTO products (productname, productdesc, status)
VALUES ('Rolex', 'Rolex watch', '123')";

//get Last Inserted Record ID
	$conn->exec($sqlQuery);
    $last_id = $conn->lastInsertId();
    echo "Your New record created successfully. your Last inserted ID is: " . $last_id;

mysql_query('INSERT INTO products(a) VALUES(\'b\')');
$id = mysql_insert_id();

Demo Example