Check if db->update successful with Codeigniter when potentially no rows are updated

↘锁芯ラ 提交于 2020-01-10 12:04:28

问题


I've been using $this->db->affected_rows() to check if updates have been successful. But this doesn't work when a user inputs the same data to be stored that is already stored (because no column is actually updated). My workaround has been to get the stored data, compare to the inputted data, update if different, return a NO_CHANGE constant if same. With JSON data, there's an extra parsing step.

Is there an easier way to check that there was no error with the update? As in, something that doesn't require getting the stored data beforehand?


回答1:


If I understand correctly, you can use transactions to ensure that there was no error with the update:

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->update('table',$array);
$this->db->trans_complete();

if ($this->db->trans_status() === FALSE)
{
    // generate an error... or use the log_message() function to log your error
}

Just remember this only validates that there were no SQL errors during all the queries inside the transaction.

Here's the link for more info Code Igniter Active Record Transaction




回答2:


Additionaly to the accepted answer solution you could expand the code like the following to differentiate affected_rows and trans_status results/errors.

$this->db->trans_start();
$this->db->query('AN SQL QUERY...');
$this->db->update('table',$array);
$this->db->trans_complete();
// was there any update or error?
if ($this->db->affected_rows() == '1') {
    return TRUE;
} else {
    // any trans error?
    if ($this->db->trans_status() === FALSE) {
        return false;
    }
    return true;
}



回答3:


Mirov When we are working with codeigniter, the data only updated when there is some change in the input field's value and then the $this->db->affected_rows() will return value greater then 0.

Suppose we have two field name and email, if we try to submit the form without changing any of the field then $this->db->affected_rows() will return 0 else it will return 1.

so better approach is to use

if($this->db->affected_rows() >=0){
  return true; //add your code here
}else{
  return false; //add your your code here
}



回答4:


my "workaround" which is pretty easy -- is to include a field that is always updated. for example if you have a field and update it with a date & time value with seconds -- then the date time seconds will always be different even if the other update values are still the same.

bonus is you have a date time in the db record for when the last update was done.




回答5:


I am not sure why we can't just use this:

if ($this->db->update('table',$array) === FALSE){
    // generate an error... or use the log_message() function to log your error
}


来源:https://stackoverflow.com/questions/20030642/check-if-db-update-successful-with-codeigniter-when-potentially-no-rows-are-upd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!