简体   繁体   English

我想要受影响的行数据和受 codeigniter 中的更新查询影响的列

[英]I want affected rows data and column which has affected by update query in codeigniter

Is there a way to obtain only the affected columns after an update query?有没有办法在更新查询后仅获取受影响的列? After an update query, that involves 10 columns, I know that only 2 columns are really updated (because the values of the other cols don't change).在涉及 10 列的更新查询之后,我知道只有 2 列真正更新(因为其他列的值没有改变)。 I need to know the names of the changed cols, for logging purpose.为了记录目的,我需要知道更改后的列的名称。

$this->db->query("UPDATE `table1` SET name = $name,......................,column10=10 WHERE id=$id")->result();
$this->db->affected_rows();

This code gives number of affected rows but i want the data and updated column information.这段代码给出了受影响的行数,但我想要数据和更新的列信息。

As far as I know there are no built-in CodeIgniter functionality for that.据我所知,没有内置的 CodeIgniter 功能。 You could compare old value with new value though :您可以将旧值与新值进行比较:

$last_record = $this->db->get_where('table1', array('id' => $id))->row_array();
$this->db->query("UPDATE `table1` SET name = $name,......................,column10=10 WHERE id=$id")->result();
// $this->db->affected_rows();
$new_record = $this->db->get_where('table1', array('id' => $id))->row_array();
$affected_columns = array_diff($new_record, $last_record);
foreach ($affected_columns as $field => $value) {
    echo "[field] : $field\r\n";
    echo "[old_value] : $last_record[$field]\r\n";
    echo "[new_value] : $value\r\n";
    echo "\r\n";
}

This will print each of the of the updated column with it's old-new value respectively.这将分别打印每个更新列的新旧值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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