【问题标题】:codeigniter oracle get insert_id()codeigniter oracle 获取 insert_id()
【发布时间】:2013-05-23 07:19:00
【问题描述】:

使用 Oracle 数据库在 CodeIgniter 中工作。现在的问题是 $this->db->insert_id();虽然它在 MySQL DB 中完美运行,但在这里不起作用。 错误报告是:

发生数据库错误

This feature is not available for the database you are using.

Filename: D:\xampp\htdocs\spiceram\system\database\drivers\oci8\oci8_driver.php

Line Number: 503

我怎样才能得到最后一个插入ID;

【问题讨论】:

标签: oracle codeigniter insert-id


【解决方案1】:

试试这样:

public function save($table, $data)
{
    $res = $this->db->insert($table, $data);

    //First select id attribute name
    $query = $this->db->get($table);
    $row = array_keys($query->row_array());
    $id_attr = $row[0];

    //Last Inserted id
    if($res)
    {
        $query2 = $this->db->query("select MAX($id_attr) from $table");
        $row2 = array_values($query2->row_array());
        $id = $row2[0];
        return $id;
    }
    else
    {
        return false;
    }
}

function insert($tableName, $post)
    {
        //First insert data
        $res = $this->db->insert($tableName, $post);

        //Get inserted id
        if($res){
            $row = $this->db->query("SELECT ID FROM $tableName ORDER BY ID DESC OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY")->row();
            $id = $row->ID;
            return $id;
        }else{
            return false;
        }

    }

【讨论】:

    【解决方案2】:

    你可以使用这个 cod,它的代码返回 ID。

    public function new_req($emp_id, $dep_id, $note)
    {
        $query = $this->db->query('SELECT MAX(REQ_ID)+1 as ID FROM REQ');
        $row = $query->row();
        $id = $row->ID;
    
        if($id == NULL) $id = 1; 
    
        $data["REQ_ID"]   =$id;
        $data["EMP_ID"]   =$emp_id;
        $data["DEP_ID"]      =$dep_id; 
        $data["REQ_TYPE"]   =1;
        $data["REQ_NOTE"]   =$note;
        $data["IP"]           =$_SERVER['REMOTE_ADDR'];
    
        $this->db->insert("REQ",$data);
        return $id;
    }
    

    【讨论】:

      【解决方案3】:
      private function insert() {
        $this->ID = $this->getNextId();
        $this->db->insert($this->getTableName(), $this);
        $this->log($this->ID);
      }
      private function getNextId() {
        $this->db->select($this->getTableName()."_SEQUENCE.NEXTVAL AS NEXTID", FALSE);
        $this->db->from("dual");
        $query = $this->db->get();
        $row = $query->row();
        return $row->NEXTID;
      } 
      

      希望这能解决 Oracle 数据库的问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 2011-10-20
        • 1970-01-01
        • 2018-02-18
        • 2011-07-31
        相关资源
        最近更新 更多