开发者

Select Row with CodeIgniter Active Record

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜