PDO get the last ID inserted

pdo last insert id returns 0
pdo get last inserted row
pdo last update id
php mysqli last insert id
mysql last insert id
last insert id php
call to undefined method pdostatement::lastinsertid()
pdo insert

I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing.

I know that I have to use this statement:

LAST_INSERT_ID()

That statement works with a query like this:

$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";

But if I want to get the ID using this statement:

$ID = LAST_INSERT_ID();

I get this error:

Fatal error: Call to undefined function LAST_INSERT_ID()

What am I doing wrong?

That's because that's an SQL function, not PHP. You can use PDO::lastInsertId().

Like:

$stmt = $db->prepare("...");
$stmt->execute();
$id = $db->lastInsertId();

If you want to do it with SQL instead of the PDO API, you would do it like a normal select query:

$stmt = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt->fetchColumn();

PDO get the last ID inserted, That's because that's an SQL function, not PHP. You can use PDO::lastInsertId() . Like: $stmt = $db->prepare(""); $stmt->execute(); $id  We retrieved the ID of the last inserted row by using PDO’s lastInsertId method. Finally, we printed out the ID for example purposes. Using lastInsertId with transactions. If you are using database transactions with the PDO extension, be warned that you will need to call the lastInsertId method BEFORE you commit the changes. If you attempt to get the ID of the last inserted row after you have committed the changes, then you will receive the value ‘0’.

lastInsertId() only work after the INSERT query.

Correct:

$stmt = $this->conn->prepare("INSERT INTO users(userName,userEmail,userPass) 
                              VALUES(?,?,?);");
$sonuc = $stmt->execute([$username,$email,$pass]);
$LAST_ID = $this->conn->lastInsertId();

Incorrect:

$stmt = $this->conn->prepare("SELECT * FROM users");
$sonuc = $stmt->execute();
$LAST_ID = $this->conn->lastInsertId(); //always return string(1)=0

PHP MySQL Get Last Inserted ID, Get ID of The Last Inserted Record We also echo the last inserted ID: $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username,  If a sequence name was not specified for the name parameter, PDO::lastInsertId() returns a string representing the row ID of the last row that was inserted into the database. If a sequence name was specified for the name parameter, PDO::lastInsertId() returns a string representing the last value retrieved from the specified sequence object.

You can get the id of the last transaction by running lastInsertId() method on the connection object($conn).

Like this $lid = $conn->lastInsertId();

Please check out the docs https://www.php.net/manual/en/language.oop5.basic.php

PDO: Get the ID of the last inserted row., This is a short guide on how to get the ID of the last inserted row using PDO's lastInsertId method. In case you didn't already know, PDO is a popular PHP  Support for PDO was added in version 2.0 of the Microsoft Drivers for PHP for SQL Server. Between version 2.0 and 4.3, the optional parameter is a table name, and the return value is the ID of the row most recently added to the provided table.

PHP, Retrieving the last inserted id $id = $pdo->lastInsertId(); // return value is an integer. In postgresql and oracle, there is the RETURNING Keyword, which returns  PDO get the last inserted ID I have a query, and I want to get the last ID inserted. The field ID is the primary key and auto incrementing. PDO get the last

How to get the last inserted record on a table with pdo_mysql , $stmt = $dbh->prepare($sql); $stmt->bindParam(':val', $val, PDO::PARAM_INT); $stmt->execute(); $lastId = $dbh->lastInsertId(); will return the last inserted id,  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

PDO::lastInsertId, I can't find any information on how to get the last inserted record with pdo_mysql. The phpdelusion site references using [PDO::lastInsertId] but it will return the last inserted id, whether the record was replaced or simply  "LAST_INSERT_ID() (no arguments) returns the first automatically generated value successfully inserted for an AUTO_INCREMENT column as a result of the most recently executed INSERT statement." This is clearly not what lastInsertId's own documentation states.

Comments
  • yes you right , i found it and it works ,thank you , i will accept answer
  • @rybo111, first it's Screaming snake case. second, it's MySQL naming convention and is not PHP
  • @rybo111, hmm SELECT LAST_INSERT_ID() is a MySQL function
  • So perhaps this is a dumb follow up question but I just want to be certain. When I rely on lastInsertId() does it return the last inserted ID that happened within my currently executing code right then? Or does it return the last inserted ID from across my database? For example, if I have a heavily trafficked website and I insert something during my login script and use lastInsertId() to get the inserted iD, but a lot of people are logging in at the same time, am I safe on relying on lastInsertId() to get the last inserted ID from that specific instance of code executing? Or do I run a risk
  • @ArtGeigel It will be the last inserted ID of the connection underlying the PDO instance. In other words, it's safe in the scenario you described since concurrent queries would take place in separate connections.
  • Welcome to Stack Overflow. We value useful contributions but please do not duplicate existing answers.