Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 270
user1014152 Avatar asked Oct 26 '11 08:10

user1014152


2 Answers

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');
like image 154
Damien Pirsy Avatar answered Sep 28 '22 06:09

Damien Pirsy


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"));
like image 25
fire Avatar answered Sep 28 '22 05:09

fire