简体   繁体   中英

PDO lastInsertId()

I'm using PDO on a MySQL database and try to retrive lastInsertId() .

I thought I was going to get the last inserted vlue in column id in my table which is my primary_key . However, all I get is 0 as output

        $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() . You can use the method after inserting record/records to collect last insert id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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