Getting insert id with insert PDO MySQL

后端 未结 2 1181
一生所求
一生所求 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();
    
    0 讨论(0)
  • 2020-12-18 20:24

    You're probably looking for lastInsertId. "Returns the ID of the last inserted row or sequence value".

    $insertedId = $system->db->lastInsertId() ;
    
    0 讨论(0)
提交回复
热议问题