how to get last insert id in php?

0
()

Today, We want to share with you how to get last insert id in php.In this post we will show you get last insert id in php mysqli, hear for how to get id from another table in php? we will give you demo and example for implement.In this post, we will learn about Laravel 5.8 – Get Last Inserted ID with an example.

PHP MySQL Get Last Inserted ID

In the table “allProducts”, the “id” column is an AUTO_INCREMENT field:

CREATE TABLE allProducts (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
product_name VARCHAR(30) NOT NULL,
product_desc VARCHAR(30) NOT NULL,
pcode VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)

Example (MySQLi Object-oriented)

<?php
$servername = "127.0.0.1:8080";
$dbUser = "rootbig";
$dbPasswrod = "dff8d$%4fdf";
$dbname = "myDB";


$link_new = new mysqli($servername, $dbUser, $dbPasswrod, $dbname);

if ($link_new->connect_error) {
  die("Connection failed: " . $link_new->connect_error);
}

$query_s = "INSERT INTO allProducts (product_name, product_desc, pcode)
VALUES ('Jayshukh', 'Dhameliya', 'P00054')";

if ($link_new->query($query_s) === TRUE) {
  $last_id = $link_new->insert_id;
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
  echo "Error: " . $query_s . "<br>" . $link_new->error;
}

$link_new->close();
?>

Example (MySQLi Procedural)

<?php
$servername = "127.0.0.1:8080";
$dbUser = "rootonly";
$dbPasswrod = "78dhye87/";
$dbname = "myDB";


$link_new = mysqli_connect($servername, $dbUser, $dbPasswrod, $dbname);

if (!$link_new) {
  die("Connection failed: " . mysqli_connect_error());
}

$query_s = "INSERT INTO allProducts (product_name, product_desc, pcode)
VALUES ('Jayshukh', 'Dhameliya', '[email protected]')";

if (mysqli_query($link_new, $query_s)) {
  $last_id = mysqli_insert_id($link_new);
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
  echo "Error: " . $query_s . "<br>" . mysqli_error($link_new);
}

mysqli_close($link_new);
?>

Free Live Chat for Any Issue

Example (PDO)

<?php
$servername = "127.0.0.1:8080";
$dbUser = "rootbhai";
$dbPasswrod = "#defdf89dfdf";
$dbname = "myDBPDO";

try {
  $link_new = new PDO("mysql:host=$servername;dbname=$dbname", $dbUser, $dbPasswrod);

  $link_new->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  $query_s = "INSERT INTO allProducts (product_name, product_desc, pcode)
  VALUES ('Jayshukh', 'Dhameliya', 'p0001')";

  $link_new->exec($query_s);
  $last_id = $link_new->lastInsertId();
  echo "New record created successfully. Last inserted ID is: " . $last_id;
} catch(PDOException $e) {
  echo $query_s . "<br>" . $e->getMessage();
}

$link_new = null;
?>

Download

I hope you get an idea about How to check whether an array is empty using PHP?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  How to Check check php version command line linux

How useful was this post?

Click on a star to rate it!