Re: Last ID Problem

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: Last ID Problem
Дата
Msg-id 20050201013855.GA52388@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: Last ID Problem  (<operationsengineer1@yahoo.com>)
Ответы Re: Last ID Problem  (Mitch Pirtle <mitch.pirtle@gmail.com>)
Список pgsql-novice
On Mon, Jan 31, 2005 at 03:33:02PM -0800, operationsengineer1@yahoo.com wrote:

> $cust = $_POST['cust'];
> $cust = addslashes($cust);
> $db = &ADONewConnection('postgres');
> $db -> Connect($db_string,$db_owner,$db_pw,$db_name);
> $sql = "INSERT INTO customer (customer_name) VALUES
> ('$cust')";
> $result = $db->Execute($sql);
> $insert_id = $db->getone("select currval('cust_id')");

If cust_id was defined as a serial type then you should be calling
currval() with the sequence name, not the column name.  Look at the
table definition (e.g., run "\d customer" in psql) and see what the
sequence name is.  It's probably customer_cust_id_seq; if so, then
following should work:

$insert_id = $db->getone("select currval('customer_cust_id_seq')");

Contrary to what another message in this thread says, it is indeed
common practice to do the insert first and call currval() afterwards
to find out what value you got from the sequence.  And no, this
doesn't introduce a race condition -- currval() returns the last
value obtained from the sequence in the current session.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

В списке pgsql-novice по дате отправления:

Предыдущее
От: Mitch Pirtle
Дата:
Сообщение: Re: Last ID Problem
Следующее
От: Michael Fuhr
Дата:
Сообщение: Re: Last ID Problem