簡體   English   中英

如何使用PHP獲取Oracle 11g XE中最后插入的記錄的序列ID?

[英]how to get sequence id of last inserted record in oracle 11g xe 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;
        }

在插入語句之后,您可以select id.currval from dual執行select id.currval from dual 這應該給您最新的價值。 請注意,這僅在獲取nextval后才能在當前會話中使用,並且不能單獨使用。

將以下內容添加到您的插入SQL查詢中:

returning id into :id

例:

$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';

並將其綁定到您的聲明

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

現在$id將在執行oci_execute($this->stmt)之后具有最后插入的ID;

此方法適用於OCI8。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM