简体   繁体   English

PDO lastInsertId()

[英]PDO lastInsertId()

I'm using PDO on a MySQL database and try to retrive lastInsertId() . 我在MySQL数据库上使用PDO并尝试检索lastInsertId()

I thought I was going to get the last inserted vlue in column id in my table which is my primary_key . 我以为我要在表的列id中获取最后插入的vlue,即我的primary_key However, all I get is 0 as output 但是,我得到的只是0作为输出

        $statement = "SELECT LAST_INSERT_ID() FROM MY_TABLE";
        $sql = $dbh->query($statement);
        $lastId = $sql->fetch(PDO::FETCH_NUM);
        $lastId = $lastId[0];
        return $lastId;

PDO class has a method called lastInsertId() . PDO类具有一个名为lastInsertId()的方法。 You can use the method after inserting record/records to collect last insert id. 您可以在插入一个或多个记录后使用该方法来收集最后的插入ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM