PHP uses pods to access the mysql database. how to return the id idphp & nbsp; of the last inserted record and access the mysql database using & nbsp; POD & nbsp, how to return the auto-increment ID of the previous insert record share to: PHP uses POD to access mysql database, how to return the auto-increment ID of the previous insert record
PHP uses pods to access the mysql database. how does one return the auto-increment ID of the last inserted record?
Share: More
------ Solution --------------------
String PDO: lastInsertId ([string $ name = NULL])
------ Solution --------------------
Try {
$ Dbh = new PDO ('MySQL: host = localhost; dbname = test', 'username', 'password ');
$ Stmt = $ dbh-> prepare ("insert into test (name, email) VALUES (?,?) ");
Try {
$ Dbh-> beginTransaction ();
$ Tmt-> execute (array ('user', [email protected] ');
$ Dbh-> commit ();
Print $ dbh-> lastInsertId ();
} Catch (PDOExecption $ e ){
$ Dbh-> rollback ();
Print "Error! : ". $ E-> getMessage ()."
";
}
} Catch (PDOExecption $ e ){
Print "Error! : ". $ E-> getMessage ()."
";
}
?>