php 获取最后更新记录的id
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20310298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the id of the last updated record
提问by Kamran Ahmed
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 i.e. $this->db->insert_id();but it doesn't work (returns 0 instead).
我能够$this->db->insert_id();在codeigniter中使用最后插入的id ,有什么方法可以获得最后更新记录的id吗?我用相同的 ie 尝试过,$this->db->insert_id();但它不起作用(而是返回 0)。
回答by Kamran Ahmed
Codeigniter doesn't support that. I had to do this:
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);
回答by user2936213
$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:
这将只给出插入的 id。要获取更新的行 id,您可以添加一列作为上次修改(时间戳),并在每次运行更新查询时使用当前时间戳更新此列。在您的更新查询之后,只需运行以下命令:
$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.
您将在结果集中获得 id。
回答by Muhammad Raheel
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;
回答by Daniel Adenew
Using a codeigniter , and MY_MODEL an extend version.This was one of the bottlneck this how i got a relfe.
使用 codeigniter 和 MY_MODEL 扩展版本。这是我如何获得 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.
调用此更新加上获取更新的 id。我想两次运行查询有点矫枉过正,但上述所有内容也是如此。
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);
回答by Mithun Billara
Return the id which your using in where clause for update
返回您在 where 子句中使用的 id 进行更新
function Update($data,$id){
$this->db->where('id', $id);
$this->db->update('update_tbl',$data);
return $id;
}
回答by Bablu Ahmed
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;
}

