【问题标题】:Select Row with CodeIgniter Active Record使用 CodeIgniter 活动记录选择行
【发布时间】:2011-09-16 03:34:24
【问题描述】:

我想构建一个类似的查询

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

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

  1. 我将日期存储在 last_attempt_time 中,格式为 DATETIME (2011-06-16 10:29:23) 我可以直接选择一些时差小于 24 小时的行吗? (或者我是否必须选择一行,然后检查例如 PHP 的时差?)

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

谢谢。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    试试这个:

    $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。

    【讨论】:

      【解决方案2】:

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

      $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');
      

      对不起,作为答案,因为 cmets 不允许 \n。

      【讨论】:

        猜你喜欢
        • 2014-03-24
        • 1970-01-01
        • 2013-01-06
        • 2014-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 2011-06-19
        相关资源
        最近更新 更多