Question : php pdo last insert id
Answered by : distinct-dolphin-g6apvsnhqvvc
$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();
Source : https://stackoverflow.com/questions/10680943/pdo-get-the-last-id-inserted | Last Update : Wed, 23 Sep 20
Question : lastinsertId php
Answered by : ahmad-alhamada
//lastInsertId() =>returens the id of the last inserted row.
$sql="query";
$dbh="DBC";
$stmt=$dbh->praper($sql);
$stmt->excute();
$id =$dbh-> lastinsertId();
$dbh=null;
Source : | Last Update : Mon, 16 May 22
Question : pdo last id
Answered by : adventurous-anaconda-5h46jif1gz1d
Beware of lastInsertId() when working with transactions in mysql. The following code returns 0 instead of the insert id.
<?php
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() . "</br>";
}
} catch( PDOExecption $e ) {
print "Error!: " . $e->getMessage() . "</br>";
}
?>
When no exception is thrown, lastInsertId returns 0. However, if lastInsertId is called before calling commit, the right id is returned.
Source : https://www.php.net/manual/pt_BR/pdo.lastinsertid.php | Last Update : Sun, 22 Mar 20