ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

10-MySQL Last Inserted

In our previous lesson we tackle about how to insert record in a MySQL database table. This time we’re going to get the last Inserted ID from our table “tblpeople“. Using the AUTO_INCREMENT field, we are able to get the ID of last inserted or updated record immediately.

Since we will be using same code that we use from our previous topic called “09-MySQL Insert” we only need to add single line of code for retrieving and echo the Last Inserted ID.

 

Example Using MySQLi(Object-Oriented):

  1. <?php
  2.  
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "mysqltutorial";
  7.  
  8. // Create connection
  9. $conn = new mysqli($server, $username, $password, $database);
  10. // Check connection
  11. if ($conn->connect_error) {
  12. die("Connection failed: " . $conn->connect_error);
  13. }
  14.  
  15. // sql to Insert record table
  16. $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  17. VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  18.  
  19. if ($conn->query($sql) === TRUE) {
  20.     $lastinsertedid = $conn->insert_id;
  21.  echo "New record created successfully! And the last record inserted is:" . $lastinsertedid;
  22. } else {
  23.  echo "Error: " . $sql . "<br>" . $conn->error;
  24. }
  25.  
  26. $conn->close();
  27.  
  28. ?>

 

Example Using MySQLi(Procedural):

  1. <?php
  2.  
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "mysqltutorial";
  7.  
  8. // Create connection
  9. $conn = mysqli_connect($server, $username, $password, $database);
  10. // Check connection
  11. if (!$conn) {
  12.     die("Connection failed: " . mysqli_connect_error());
  13. }
  14.  
  15. // sql to Insert record table
  16. $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  17. VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  18.  
  19. if (mysqli_query($conn, $sql)) {
  20.      $lastinserteid = mysqli_insert_id($conn);
  21.     echo "New record created successfully! And the last Inserted ID is : ". $lastinserteid;
  22. } else {
  23.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  24. }
  25.  
  26. mysqli_close($conn);
  27. ?>

 

Example Using PDO:

  1. <?php
  2.  
  3. $server = "localhost";
  4. $username = "root";
  5. $password = "";
  6. $database = "mysqltutorial";
  7.  
  8. try {
  9.     $conn = new PDO("mysql:host=$server;dbname=$database", $username, $password);
  10.     // set the PDO error mode to exception
  11.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12.  
  13.     // sql to Insert record table
  14.     $sql = "INSERT INTO `tblpeople` (`LNAME`, `FNAME`, `ADDRESS`)
  15.     VALUES ('Villanueva', 'Joken', 'Kabankalan City');";
  16.  
  17.     // use exec() because no results are returned
  18.     $conn->exec($sql);
  19.     $lastinsertedid = $conn->lastInsertId();
  20.     echo "New record created successfully! And the last Inserted ID is :" . $lastinsertedid;
  21.     }
  22. catch(PDOException $e)
  23.     {
  24.     echo $sql . "<br>" . $e->getMessage();
  25.     }
  26.  
  27. $conn = null;
  28. ?>

 

Facebook Comments

Check Your Domain Ranking

Leave a Reply

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

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!