pg_last_oid

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

pg_last_oid返回上一条记录的 oid

说明

pg_last_oid(PgSql\Result $result): string|int|false

pg_last_oid() 用于检索分配给插入行的 OID

从 PostgreSQL 7.2 开始 OID 字段成为可选项,在 PostgreSQL 8.1 中默认不存在。如果表中没有定义 OID 字段,程序员必须用 pg_result_status() 检查记录是否成功插入。

要在插入的行中获取 SERIAL 字段的值,必须使用 PostgreSQL CURRVAL 函数,命名需要最后一个值的序列。如果序列名称未知,则需要 pg_get_serial_sequence PostgreSQL 8.0 函数。

PostgreSQL 8.1 有一个函数 LASTVAL,它返回会话中最近使用的序列的值。这避免了对序列、表或列的命名需求。

注意:

本函数以前的名字为 pg_getlastoid()

参数

result

PgSql\Result 实例,由 pg_query()pg_query_params() 或者 pg_execute()(等)返回。

返回值

intstring,包含分配给指定 connection 中最近插入的行的 OID,如果出错或没有可用的 OID,则为 false

更新日志

版本 说明
8.1.0 现在 result 参数接受 PgSql\Result 实例,之前接受 resource

示例

示例 #1 pg_last_oid() 示例

<?php
// 连接到数据库
pg_connect("dbname=mark host=localhost");

// 创建示例表
pg_query("CREATE TABLE test (a INTEGER) WITH OIDS");

// 插入一些数据
$res = pg_query("INSERT INTO test VALUES (1)");

$oid = pg_last_oid($res);
?>

参见

add a note add a note

User Contributed Notes 6 notes

up
0
julian at e2-media dot co dot nz
21 years ago
The way I nuderstand it, each value is emitted by a sequence only ONCE. If you retrieve a number (say 12) from a sequence using nextval(), the sequence will advance and subsequent calls to nextval() will return further numbers (after 12) in the sequence.

This means that if you use nextval() to retrieve a value to use as a primary key, you can be guaranteed that no other calls to nextval() on that sequence will return the same value. No race conditions, no transactions required.

That's what sequences are *for* afaik :^)
up
0
a dot bardsley at lancs dot ac dot uk
21 years ago
As pointed out on a busy site some of the above methods might actually give you an incorrect answer as another record is inserted inbetween your insert  and the select. To combat this put it into a transaction and dont commit till you have done all the work
up
0
webmaster at gamecrash dot net
21 years ago
You could use this to get the last insert id...

CREATE TABLE test (
  id serial,
  something int not null
);

This creates the sequence test_id_seq. Now do the following after inserting something into table test:

INSERT INTO test (something) VALUES (123);
SELECT currval('test_id_seq') AS lastinsertid;

lastinsertid should contain your last insert id.
up
-2
dtutar at yore dot com dot tr
21 years ago
This is very useful function :)

function sql_last_inserted_id($connection, $result, $table_name, $column_name) {
   $oid = pg_last_oid ( $result);
       $query_for_id = "SELECT $column_name FROM $table_name WHERE oid=$oid";
   $result_for_id = pg_query($connection,$query_for_id);
   if(pg_num_rows($result_for_id))
      $id=pg_fetch_array($result_for_id,0,PGSQL_ASSOC);
   return $id[$column_name];
}

Call after insert, simply ;)
up
-3
qeremy [atta] gmail [dotta] com
12 years ago
Simply getting LAST_INSERT_ID;

<?php
// Note: waiting for "select" part from pg_query below.
// Note: separating the query parts using semicolons (;).

$qry = pg_query("
    INSERT INTO users (id,uname,upass,rep) VALUES (DEFAULT,'bubu','a981v',0.19);
    SELECT Currval('users_id_seq') LIMIT 1
"
);

// or
$qry = pg_query("
    INSERT INTO users (id,uname,upass,rep) VALUES (DEFAULT,'bubu','a981v',0.19) RETURNING Currval('users_id_seq')"
);

$fch = pg_fetch_row($qry);
print_r($fch);
?>

Array
(
    [0] => 3 -> Gotcha!
)
up
-3
Jonathan Bond-Caron
19 years ago
I'm sharing an elegant solution I found on the web (Vadim Passynkov):

CREATE RULE get_pkey_on_insert AS ON INSERT TO Customers DO SELECT currval('customers_customers_id_seq') AS id;

Every time you insert to the Customers table, postgreSQL will return a table with the id you just inserted. No need to worry about concurrency, the ressource is locked when the rule gets executed.

Note that in cases of multiple inserts:
INSERT INTO C1 ( ... ) ( SELECT * FROM C2);

we would return the id of the last inserted row.

For more info about PostgreSQL rules:
http://www.postgresql.org/docs/7.4/interactive/sql-createrule.html
To Top