I'm trying to use CodeIgniter's database classes to decrease a value of a column. At the moment I'm doing this:
public function deduct_limit($bytes, $ip_address) {
$this->db->where('ip_address', $ip_address);
$this->db->set('limit', 'limit - ' . $bytes, FALSE);
$this->db->update('limits');
}
However, CodeIgniter throws up this error message:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'limit - 418266480 WHERE
ip_address
= '127.0.0.1'' at line 1
UPDATE `limits`
SET `limit` = limit - 418266480
WHERE `ip_address` = '127.0.0.1'
As far as I know that SQL is correct, I've Google'd it and looked on SO and they all follow that syntax. Can I not use this with anything other than +1
or -1
?
You need to escape reserved words in MySQL like limit
with backticks
UPDATE limits
SET `limit` = `limit` - 418266480
WHERE ip_address = '127.0.0.1'
this may help you
$this->db->set('limit', 'limit - ' . (int) $bytes, FALSE);