PDO lastInsertId对事务不起作用?

Jas*_*vis 16 php mysql pdo

我第一次使用PDO,只是在玩它.

到目前为止,当我尝试在事务中执行插入时...

$this->dbh->beginTransaction();
// $sql query ran
$this->dbh->commit();

echo $this->dbh->lastInsertId();
Run Code Online (Sandbox Code Playgroud)

lastInsertId()返回0 ...当我在事务外运行相同的查询时,我得到正确的id号返回.这里有什么我想念的吗?

Sta*_*arx 30

lastInsertId()在提交之前你必须要求transaction

尝试

$this->dbh->beginTransaction();
// $sql query ran
echo $this->dbh->lastInsertId();
$this->dbh->commit();
Run Code Online (Sandbox Code Playgroud)

  • 这让我免于跳出窗户。谢谢! (2认同)