php - PHP/MYSQL对更新字段的操作

标签 php mysql codeigniter if-statement

我遇到了一个小问题,我需要生成一个报告,重点关注数据库中某些字段的更改。我想做的是这样的

“如果已更新,请在单独的字段中插入今天的日期”

这样,在生成报告时,可以通过检查日期来验证哪些条目已被修改。

问题是,我不确定如何使用php / codeigniter做到这一点

感谢您的提示!

最佳答案

您可以将时间戳字段添加到表结构中

your_field timestamp default current_timestamp on update current_timestamp


查看手册中的可用语法

http://dev.mysql.com/doc/refman/5.0/en/timestamp.html

编辑。
如果仅在特定字段更改时才需要更新时间,则需要触发器。

create table test (
id int not null auto_increment primary key,
field1 int,
field2 int,
field3 int,
last_update datetime)
engine = myisam;


delimiter //
drop trigger if exists update_time //
create trigger update_time before update on test
for each row begin
    if new.field2 <> old.field2 or new.field3 <> old.field3 then
        set new.last_update = now();
    end if;
end //
delimiter ;


在我的示例中,只有在field2或field3更改时,last_update字段才会使用当前时间进行更新。

关于php - PHP/MYSQL对更新字段的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6244606/

相关文章:

mysql - 在 mysql 查询中显示计数器

php - CodeIgniter 从传递单行到传递多行

javascript - 500 错误状态 : using angular js and codeigniter 2. 2

php - 带有准备语句的 PostgreSQL 内部查询

php - 从wordpress主题创建octopress主题

python - mysql.connector.errors.ProgrammingError : Not all parameters were used in the SQL statement (Python, MySQL)

php - 在 MySQL 中按特定关键字对结果进行分组?

php - 查询代码点火器

php - 使用php基于mysql查询自动回复电子邮件

php - 使用 php 在 json 中搜索