简体   繁体   中英

Get the id of the last updated record

I am able to get the last inserted id using $this->db->insert_id(); in codeigniter, is there any way that I can get the id of the last updated record? I tried it with the same ie $this->db->insert_id(); but it doesn't work (returns 0 instead).

Codeigniter doesn't support that. I had to do this:

$updated_id = 0;

// get the record that you want to update
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$query = $this->db->get('StockMain');

// getting the Id
$result = $query->result_array();
$updated_id = $result[0]['stid'];

// updating the record
$this->db->where(array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale'));
$this->db->update('StockMain',$data);
$this->db->insert_id();  

this will give only inserted id. For getting the updated row id you can add a column as lastmodified (timestamp) and update this column with current timestamp everytime you run the update query. After your update query just run this:

$query = $this->db->query('SELECT id FROM StockMain ORDER BY lastmodified DESC LIMIT 1');  
$result = $query->result_array();  

You will get the id in the result set.

Here is how you can do it shortest

$where  =   array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale');

//Update the record

$this->db->where($where);
$this->db->update('StockMain',$data);

//Get the record

$this->db->where($where);
return $this->db->get('StockMain')->row()->stid;

Using a codeigniter , and MY_MODEL an extend version.This was one of the bottlneck this how i got a relfe.

  function update_by($where = array(),$data=array())
  {
        $this->db->where($where);
        $query = $this->db->update($this->_table,$data);
        return $this->db->get($this->_table)->row()->id; //id must be exactly the name of your table primary key
  }

call this Updates plus get the updated id. kinda overkill to run a query twice i guess but so do all the aboves.

how you call ?

 $where = array('ABC_id'=>5,'DEF_ID'=>6);
 $data =  array('status'=>'ACCEPT','seen_status' =>'SEEN');
 $updated_id= $this->friends->update_by($where,$data);

Return the id which your using in where clause for update

function Update($data,$id){
        $this->db->where('id', $id);
        $this->db->update('update_tbl',$data);
        return $id; 
    }

Try like this:

  //update
    public function update($table, $where, $data)
    {
        // get the record that you want to update
        $this->db->where($where);
        $query = $this->db->get($table);

        // getting the Id
        $row = array_values($query->row_array());
        $updated_id = $row[0];

        // updating the record
        $updated_status = $this->db->update($table, $data, $where);

        if($updated_status):
            return $updated_id;
        else:
            return false;
        endif;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM