简体   繁体   English

使用CodeIgniter Active Record选择Row

[英]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. 我有2个问题来构建上述查询。

  1. I'm storing the date in last_attempt_time in DATETIME format (2011-06-16 10:29:23) Can I directly select some row which has time difference less than 24 hours from now? 我在DATETIME格式的last_attempt_time中存储日期(2011-06-16 10:29:23)我可以直接选择从现在起不到24小时的时间差的行吗? (OR do I have to select a row, and then check with for example PHP the time difference? ) (或者我必须选择一行,然后检查PHP的时差吗?)

  2. How can I write this query using CodeIgniter Active Record? 如何使用CodeIgniter Active Record编写此查询? (Do I need to use get_where?) I could not find any related example in their documentation. (我需要使用get_where吗?)我在他们的文档中找不到任何相关的例子。

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 . 您可以在此处找到完整的文档: http//codeigniter.com/user_guide/database/active_record.html#select

an small optimization to Francois's answer: 对Francois答案的一个小优化:

$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. 对不起,作为答案,因为评论不允许\\ n。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM