当可能没有行更新时,使用 Codeigniter 检查 db->update 是否成功 [英] Check if db->update successful with Codeigniter when potentially no rows are updated

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

问题描述

我一直在使用 $this->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 Igniter Active Record Transaction

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

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

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