Question

je reçois cette erreur dans PDO:

Erreur: Message: PDO :: lastInsertId () [pdo.lastinsertid]: SQLSTATE [IM001]: Le pilote ne prend pas en charge cette fonction: pilote ne pas soutenir lastInsertId ()

en essayant d'obtenir la dernière id inséré à partir d'une base de données Oracle. J'ai ajouté la chaîne de séquence à la dernière fonction id insert, mais ne fonctionne toujours pas. Google ne dit pas grand-chose au sujet de cette erreur sur Oracle avec PDO.

Était-ce utile?

La solution

Oracle doesn't have autoincrement columns, so lastInsertId isn't supported in the same way as for MySQL. You have to implement the equivalent "by hand" using Oracle sequences.

Create an oracle sequence for every table that requires it, and use NEXTVAL to retrieve it whenever you need to do an insert, then use that value when inserting in 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));

Autres conseils

In Oracle, you should have created an oracle sequence for tables which requires an auto increment column.

If you want to insert, then you can do it in the same time rather than query nextIncrementId and insert as below:

$sh = $conn->prepare("INSERT INTO table (id, data) VALUES(SEQUENCE_NAME.NEXTVAL, ?)");
$sh->execute(array($valueToBeInsertedInDataColumn));

If you just want to know the last insert id, then don't use nextval because whenever you call it, it increment the value to the next one. You should use CURRVAL for that purpose. Below is an example:

$sh = $conn->prepare("SELECT SEQUENCE_NAME.CURRVAL AS lastInsertId FROM DUAL");
$lastInserId = $sh->execute();

Debug: print_r($lastInserId);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top