Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Adodb for php/postgresql, return row ID after insert

When I insert a row, it increments the ID field using SERIAL.

How can I return the newly created ID number right after the insert?

(Database is Postgresql.)

like image 357
Nic Hubbard Avatar asked Feb 22 '10 04:02

Nic Hubbard


3 Answers

$db->Insert_ID();
like image 60
Nirmal Avatar answered Sep 22 '22 15:09

Nirmal


This code works nicely:

$insertId = $adodb->getOne("INSERT INTO test (str) values ('test') RETURNING id");
like image 33
user2116547 Avatar answered Sep 25 '22 15:09

user2116547


Treat your INSERT the same as a SELECT and use RETURNING in your INSERT-query:

INSERT INTO foo (bar) VALUES('Doe') RETURNING id;

Fetch the result and you're done. Works since version 8.2

like image 41
Frank Heikens Avatar answered Sep 25 '22 15:09

Frank Heikens