[英]Using native MySQL funciton in active records (codeigniter)
I'm trying to get all data which date is greater than or equal to today. 我正在尝试获取日期大于或等于今天的所有数据。 Here is what I do: 这是我做的:
$this->db->select('id, name');
$this->db->where('dr', '1');
$this->db->where('end >=', 'CURDATE()');
$query = $this->db->get('store');
But it doesen't work. 但它不起作用。 What I wrong ? 我错了什么?
Active Records automatically escapes your query, so CURDATE() will be passed as a string instead of using the mysql function. Active Records会自动转义您的查询,因此CURDATE()将作为字符串传递,而不是使用mysql函数。 You'd better off runing your query manually, something like 您最好手动运行查询,例如
$query = $this->db->query("SELECT id,name FROM store WHERE dr = '1' AND end >= CURDATE()");
Or if you still want to use AR, you could pass a FALSE as third parameter of $this->db->where
to avoid automatic escaping by CI: 或者如果你仍想使用AR, 你可以传递一个FALSE作为 $this->db->where
第三个参数 $this->db->where
以避免CI自动转义:
$this->db->select('id,name')
->where('dr','1')
->where('end >=', 'CURDATE()', FALSE);
$query = $this->db->get('store');
I don't use active records in CI but I would guess that CURDATE()
is being put in a string in the query so it's doing WHERE end >= 'CURDATE()'
as opposed to WHERE end >= CURDATE()
我不在CI中使用活动记录,但我猜测CURDATE()
被放入查询中的字符串中,所以它正在做WHERE end >= 'CURDATE()'
而不是WHERE end >= CURDATE()
If end
is a timestamp field try... 如果end
是时间戳字段,请尝试...
$this->db->where('end >=', time());
If end
is a datetime field try... 如果end
是datetime字段,请尝试...
$this->db->where('end >=', date("Y-m-d H:i:s"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.