开发者

Select Row with CodeIgniter Active Record

开发者 https://www.devze.com 2023-03-13 13:58 出处:网络
I want to build a query like SELECT username from users WHERE login_attempts > 3 AND last_attempt_time < 24 (hours).

I want to build a query like

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

I have 2 questions to build the above query.

  1. I'm storing the date in last_attempt_time in DATETIME format (2开发者_JS百科011-06-16 10:29:23) Can I directly select some row which has time difference less than 24 hours from now? (OR do I have to select a row, and then check with for example PHP the time difference? )

  2. How can I write this query using CodeIgniter Active Record? (Do I need to use get_where?) I could not find any related example in their documentation.

Thanks.


Try this:

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

You can find the complete documentation here: http://codeigniter.com/user_guide/database/active_record.html#select.


an small optimization to Francois's answer:

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

Sorry, put as an answer because comments don't allows \n.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号