views:

27

answers:

1

I need to increment the number of comments on an Entries Table, the SQL for this would be:

UPDATE entries SET comments = comments + 1 WHERE entry_id = 123;

And i was wondering how to do this using Active Record Class form Code Igniter.

http://codeigniter.com/user_guide/database/active_record.html#update

+2  A: 

Your question is pretty much a duplicate of this question. Setting the third parameter of set to FALSE prevents the data from being escaped.

$this->db->set('comments', 'comments+1', FALSE)
$this->db->where('entry_id', 123);
$this->db->update('entries');

There is also nothing stopping you executing the SQL directly if you find Active Record a bit clunky.

$sql = 'UPDATE entries SET comments=comments+1 WHERE entry_id=?';
$this->db->query($sql, array(123));
Stephen Curran