Online training and video tutorials
Enter     Registration
     Contacts

 
     
 
Max Azimov
St. Petersburg

Articles [97]

User's video [259]





PHP Get ID of Last Inserted Record
 
Get ID of The Last Inserted Record
 
If we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately.
 
In the table "MyGuests", the "id" column is an AUTO_INCREMENT field:
  1. CREATE TABLE MyGuests (
  2. id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  3. firstname VARCHAR(30) NOT NULL,
  4. lastname VARCHAR(30) NOT NULL,
  5. email VARCHAR(50),
  6. reg_date TIMESTAMP
  7. )
 
The following examples are equal to the examples from the previous page (PHP Insert Data Into MySQL), except that we have added one single line of code to retrieve the ID of the last inserted record. We also echo the last inserted ID:
 
Example (MySQLi Object-oriented)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = new mysqli($servername, $username, $password, $dbname);
  8. // Check connection
  9. if ($conn->connect_error) {
  10.     die("Connection failed: " . $conn->connect_error);
  11. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  12. VALUES ('John', 'Doe', 'john@example.com')";
  13. if ($conn->query($sql) === TRUE) {
  14.     $last_id = $conn->insert_id;
  15.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  16. } else {
  17.     echo "Error: " . $sql . "<br>" . $conn->error;
  18. }
  19. $conn->close();
  20. ?>
 
Example (MySQLi Procedural)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDB";
  6. // Create connection
  7. $conn = mysqli_connect($servername, $username, $password, $dbname);
  8. // Check connection
  9. if (!$conn) {
  10.     die("Connection failed: " . mysqli_connect_error());
  11. }
  12. $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  13. VALUES ('John', 'Doe', 'john@example.com')";
  14. if (mysqli_query($conn, $sql)) {
  15.     $last_id = mysqli_insert_id($conn);
  16.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  17. } else {
  18.     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  19. }
  20. mysqli_close($conn);
  21. ?>
 
Example (PDO)
  1. <?php
  2. $servername = "localhost";
  3. $username = "username";
  4. $password = "password";
  5. $dbname = "myDBPDO";
  6. try {
  7.     $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
  8.     // set the PDO error mode to exception
  9.     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  10.     $sql = "INSERT INTO MyGuests (firstname, lastname, email)
  11.     VALUES ('John', 'Doe', 'john@example.com')";
  12.     // use exec() because no results are returned
  13.     $conn->exec($sql);
  14.     $last_id = $conn->lastInsertId();
  15.     echo "New record created successfully. Last inserted ID is: " . $last_id;
  16.     }
  17. catch(PDOException $e)
  18.     {
  19.     echo $sql . "<br>" . $e->getMessage();
  20.     }
  21. $conn = null;
  22. ?>


2016-12-10
537 views




 
     
 
Map      Distance education, online learning courses. Project PLANETSTUDY - distance learning online.
© 2012 PlanetStudy

Catalog.299.ru

The site is a media provider
Яндекс.Метрика
 
English USA
Australia Русский