//Connect to database $db = new PDO("mysql:host=localhost;dbname=testdb", "username", "password"); //Insert a new user $stmt = $db->prepare("INSERT INTO users(name) VALUES (:name)"); $stmt->bindParam(':name', $name); $name = "John Doe"; $stmt->execute(); //Get the ID of the last inserted row $lastInsertId = $db->lastInsertId(); echo "The last inserted ID is ".$lastInsertId;
//Connect to database $db = new PDO("mysql:host=localhost;dbname=testdb", "username", "password"); //Create a new sequence $stmt = $db->prepare("CREATE SEQUENCE seq_user INCREMENT BY 1 START WITH 1"); $stmt->execute(); //Get the ID of the last created sequence $lastInsertId = $db->lastInsertId(); echo "The last created sequence ID is ".$lastInsertId;PHP Db lastInsertId is a method provided by PDO package library that is built-in with PHP.