欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP 使用 POD 访问mysql数据库,如何返回上一个插入记录的自增编号ID

程序员文章站 2022-06-12 16:38:32
...
PHP 使用 POD 访问mysql数据库,如何返回上一个插入记录的自增编号ID

回复讨论(解决方案)

string PDO::lastInsertId ([ string $name = NULL ] )

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', 'user@example.com'));
$dbh->commit();
print $dbh->lastInsertId();
} catch(PDOExecption $e) {
$dbh->rollback();
print "Error!: " . $e->getMessage() . "";
}
} catch( PDOExecption $e ) {
print "Error!: " . $e->getMessage() . "";
}
?>