简体   繁体   中英

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 ?

$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 . 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.

add the following to your insert SQL query:

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;

This method works well with OCI8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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