Get the id of the last updated record

后端 未结 6 1325
天涯浪人
天涯浪人 2021-01-23 02:51

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

相关标签:
6条回答
  • 2021-01-23 03:17

    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; 
        }
    
    0 讨论(0)
  • 2021-01-23 03:18

    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);
    
    0 讨论(0)
  • 2021-01-23 03:21
    $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.

    0 讨论(0)
  • 2021-01-23 03:24

    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);
    
    0 讨论(0)
  • 2021-01-23 03:32

    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;
    
    0 讨论(0)
  • 2021-01-23 03:35

    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;
        }
    
    0 讨论(0)
提交回复
热议问题