Why doesn't Oracle PDO driver use lastInsertId ()?

I get this error in PDO:

error: Message: PDO :: lastInsertId () [pdo.lastinsertid]: SQLSTATE [IM001]: the driver does not support this function: the driver does not support lastInsertId ()

when trying to get the last inserted id from oracle database. I added a sequence string to the last function of the insert identifier, but still does not work. Google does not talk about this error in Oracle with PDO.

+6
source share
1 answer

There are no auto-increment columns in Oracle, so lastInsertId is not supported in the same way as for MySQL. You must implement the equivalent manually using Oracle sequences.

Create an oracle sequence for each table that requires it and use NEXTVAL to get it when you need to insert, and then use this value when pasting into the table.

$sh = $conn->prepare('SELECT uid_seq.NEXTVAL AS nextInsertID FROM DUAL'); $sh->execute(); $nextInsertId = $sh->fetchColumn(0); $sh = $conn->prepare("INSERT INTO table (id, data) VALUES(?, 255)"); $sh->execute(array($nextInsertId)); 
+10
source

Source: https://habr.com/ru/post/908703/


All Articles