繁体   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