0

使用 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;

4

3 回答 3

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

    }
于 2018-08-01T14:44:18.510 回答
1

你可以使用这个鳕鱼,它的代码返回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;
}
于 2015-11-12T11:52:10.293 回答
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 数据库案例的问题。

于 2013-12-11T18:17:51.717 回答