检查DB->更新成功的codeigniter时可能没有行被更新 [英] Check if db->update successful with Codeigniter when potentially no rows are updated

查看:129
本文介绍了检查DB->更新成功的codeigniter时可能没有行被更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 $这个 - > DB-> affected_rows()来检查是否更新是成功的。但是,当用户输入相同的数据被存储在已存储的(因为没有列实际更新)这不工作。我的解决方法已经获得存储的数据,比较所输入的数据,如果不同的更新,返回NO_CHANGE不变,如果相同的。随着JSON数据,还有一个额外的分析步骤。

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?

推荐答案

如果我理解正确的话,你可以使用事务,以确保有一个与更新没有错误:

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
}

只要记住这只是验证,在所有事务中的查询没有SQL错误。

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

下面的链接了解更多信息 code点火器的Active Record交易

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

这篇关于检查DB->更新成功的codeigniter时可能没有行被更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆