简体   繁体   English

如何使用PHP获取Oracle 11g XE中最后插入的记录的序列ID?

[英]how to get sequence id of last inserted record in oracle 11g xe with php?

here i am trying to insert a record as well as retrive last inserted sequence id but didn't get any success can anybody help me , guide me that how oracle works with php ? 在这里,我试图插入一条记录以及检索最后插入的序列ID,但是没有成功,有人可以帮助我,指导我oracle如何与php一起工作吗?

$query = 'INSERT INTO hist_news (id,headline,reportedon,reportedby,loc,story,more,helperjson,refjson,createdt,createdby) VALUES(id.nextval, :headline, :reportedon, :reportedby , :loc , :story , :more , :helper , :ref , sysdate , :createdby) return id.nextval';
        $this->stmt = oci_parse($this->oci,$query);
        oci_bind_by_name($this->stmt,':headline',$headline);
        oci_bind_by_name($this->stmt,':reportedon',$reportedon);
        oci_bind_by_name($this->stmt,':reportedby',$reportedby);
        oci_bind_by_name($this->stmt,':loc',$loc);
        oci_bind_by_name($this->stmt,':story',$story);
        oci_bind_by_name($this->stmt,':more',$more);
        oci_bind_by_name($this->stmt,':helper',$helperjson);
        oci_bind_by_name($this->stmt,':ref',$refjson);
        if($re = oci_execute($this->stmt)){
            return $re;
        } else {
            return false;
        }

After the insert statement you can execute select id.currval from dual . 在插入语句之后,您可以select id.currval from dual执行select id.currval from dual This should give you the latest value. 这应该给您最新的价值。 Do note that this will only work only in the current session after you have fetched the nextval and cannot be used by itself. 请注意,这仅在获取nextval后才能在当前会话中使用,并且不能单独使用。

add the following to your insert SQL query: 将以下内容添加到您的插入SQL查询中:

returning id into :id

Example: 例:

$query = 'INSERT INTO hist_news (id,headline,reportedon,reportedby,loc,story,more,helperjson,refjson,createdt,createdby) VALUES(id.nextval, :headline, :reportedon, :reportedby , :loc , :story , :more , :helper , :ref , sysdate , :createdby) returning id into :id';

And bind this to your statement 并将其绑定到您的声明

oci_bind_by_name($this->stmt,":ID",$id);

Now $id will have the last inserted ID after oci_execute($this->stmt) is executed; 现在$id将在执行oci_execute($this->stmt)之后具有最后插入的ID;

This method works well with OCI8. 此方法适用于OCI8。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM