codeigniter oracle get insert_id()

时间:2013-05-28 10:42:04

标签: oracle codeigniter insert-id

使用Oracle数据库在CodeIgniter中工作。现在的问题是 $这 - > 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;

3 个答案:

答案 0 :(得分:1)

you can use this cod, it's code return 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;
}

答案 1 :(得分:0)

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数据库的问题。

答案 2 :(得分:0)

尝试这样:

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;
    }
}

OR

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;
        }

    }