CodeIgniter query: How to move a column value to another column in the same row and save the current time in the original column?(CodeIgniter 查询:如何将列值移动到同一行中的另一列并将当前时间保存在原始列中?)
问题描述
在我的 db 表中,我有两个日期时间列:Last 和 Current.这些列允许我跟踪某人上次使用有效登录名登录我正在构建的服务的时间.
In my db table, I have two datetime columns: Last and Current. These column allow me to keep track of when someone last used a valid login to the service I am building up.
使用 CodeIgniter 的活动记录,是否可以更新一行,以便 Last 值接收 Current 值,然后是 Current 值是否替换为当前日期时间?
Using CodeIgniter's active record, is it possible to update a row so that the Last value receives the Current value AND then the Current value is replace with the current datetime?
推荐答案
试试这样:
$data = array('current_login' => date('Y-m-d H:i:s'));
$this->db->set('last_login', 'current_login', false);
$this->db->where('id', 'some_id');
$this->db->update('login_table', $data);
特别注意 set() 调用的第三个参数.false 防止 CodeIgniter 引用第二个参数——这允许将值视为表列而不是字符串值.对于不需要特殊处理的任何数据,您可以将所有这些声明集中到 $data 数组中.
Pay particular attention to the set() call's 3rd parameter. false prevents CodeIgniter from quoting the 2nd parameter -- this allows the value to be treated as a table column and not a string value. For any data that doesn't need to special treatment, you can lump all of those declarations into the $data array.
以上代码生成的查询:
UPDATE `login_table`
SET last_login = current_login, `current_login` = '2018-01-18 15:24:13'
WHERE `id` = 'some_id'
这篇关于CodeIgniter 查询:如何将列值移动到同一行中的另一列并将当前时间保存在原始列中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CodeIgniter 查询:如何将列值移动到同一行中的另一列并将当前时间保存在原始列中?
基础教程推荐
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
