Php 获取上次更新记录的id

Php 获取上次更新记录的id,php,codeigniter,activerecord,codeigniter-2,Php,Codeigniter,Activerecord,Codeigniter 2,我可以使用$this->db->insert_id()获取最后一个插入的id在codeigniter中,是否有任何方法可以获取上次更新记录的id?我用同样的方法试过了,即$this->db->insert_id()但它不起作用(返回0)。Codeigniter不支持该功能。我必须这样做: $updated_id = 0; // get the record that you want to update $this->db->where(array('vrnoa'=>$dat

我可以使用
$this->db->insert_id()获取最后一个插入的id
在codeigniter中,是否有任何方法可以获取上次更新记录的id?我用同样的方法试过了,即
$this->db->insert_id()
但它不起作用(返回0)。

Codeigniter不支持该功能。我必须这样做:

$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);
这将只提供插入的id。要获取更新的行id,您可以添加一列作为lastmodified(时间戳),并在每次运行更新查询时使用当前时间戳更新此列。更新查询后,只需运行以下命令:

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

您将在结果集中获得id。

以下是您如何做到这一点的方法

$where  =   array('vrnoa'=>$data['vrnoa'], 'etype' => 'sale');
//更新记录

$this->db->where($where);
$this->db->update('StockMain',$data);
$this->db->where($where);
return $this->db->get('StockMain')->row()->stid;
//记录在案

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

使用codeigniter,我的_模型是一个扩展版本。这是我获得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
  }
调用此更新并获取更新的id。我想运行两次查询有点过火,但上面的所有操作也是如此

你怎么称呼

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

返回您在where子句中用于更新的id

function Update($data,$id){
        $this->db->where('id', $id);
        $this->db->update('update_tbl',$data);
        return $id; 
    }
试着这样做:

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

如果多个用户执行更新/相同操作,则不起作用。