关于php:使用CodeIgniter活动记录选择行

Select Row with CodeIgniter Active Record

我想建立一个像

这样的查询

1
2
SELECT username from users
 WHERE login_attempts > 3 AND last_attempt_time < 24 (hours).

我有2个问题来构建上述查询。

  • 我正在将日期以last_attempt_time的格式存储为DATETIME格式(2011-06-16 10:29:23)
    我可以直接选择时差从现在开始少于24小时的行吗? (或者我是否必须选择一行,然后使用例如PHP的时差进行检查?)

  • 如何使用CodeIgniter Active Record编写此查询? (我需要使用get_where吗?)我在他们的文档中找不到任何相关示例。

  • 谢谢。


    尝试一下:

    1
    2
    3
    4
    5
    6
    $this->db->select('username');
    $this->db->from('users');
    $this->db->where('login_attempts >', 3);
    $this->db->where('last_attempt_time <', date('Y-m-d H:i:s', strtotime('-24 hours')));

    $query = $this->db->get();

    您可以在此处找到完整的文档:http://codeigniter.com/user_guide/database/active_record.html#select。


    对弗朗索瓦的答案的一个小优化:

    1
    2
    3
    4
    5
    $this->db->select('username');
    $this->db->where('login_attempts >', 3);
    $this->db->where('last_attempt_time <', date('Y-m-d H:i:s', strtotime('-24 hours')));

    $query = $this->db->get('users');

    对不起,请回答,因为注释不允许\\\\