Getting insert id with insert PDO MySQL

后端 未结 2 1183
一生所求
一生所求 2020-12-18 19:28

Im getting to grips with the basics of PDO.

However Im trying to get the id of the inserted row, Im using:

$query = $system->db->prepare(\"INSE         


        
2条回答
  •  独厮守ぢ
    2020-12-18 20:11

    Pay attention when using transactions.

    If you call lastInsertedId after you call commit, lastInsertedId will return 0 instead of the id. Call lastInsertedId right after execute, but before commit.

    $this->db->beginTransaction();
    $this->stmt->execute();
    $id = $this->db->lastInsertId();
    $this->db->commit();
    

提交回复
热议问题