简体   繁体   中英

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. 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:

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

If end is a timestamp field try...

$this->db->where('end >=', time());

If end is a datetime field try...

$this->db->where('end >=', date("Y-m-d H:i:s"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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