How to retrieve the id of the last inserted row when using PDO in PHP?

后端 未结 1 1745
失恋的感觉
失恋的感觉 2020-12-12 05:40

Example: I insert a row into the DB with this, using PHP\'s built in PDO:

$sql = \"INSERT INTO mytable (name, ok) VALUES (\'john\', \'1\')\";
$this->dbh-&         


        
相关标签:
1条回答
  • 2020-12-12 06:21

    If the id is an auto_increment, you can use PDO::lastInsertId :

    Returns the ID of the last inserted row, or the last value from a sequence object, depending on the underlying driver.


    So, in your case, something like this should do the trick :

    $lastId = $this->dbh->lastInsertId();
    
    0 讨论(0)
提交回复
热议问题