Increment field of mysql database using codeigniter's active record syntax
Asked 07 September, 2021
Viewed 1.2K times
  • 53
Votes

I have the following php-codeigniter script which attempts to increment a field of a record using active-record syntax:

$data = array('votes' => '(votes + 1)');
$this->db->where('id', $post['identifier']);
$this->db->update('users', $data);

This produces the following SQL:

"UPDATEusersSETvotes= '(votes + 1)' WHEREid= '44'"

Which doesn't run, but this SQL does do what I'm looking for: "UPDATEusersSETvotes= (votes + 1) WHEREid= '44'"` <--Note the lack of quotes around (votes + 1)

Does anyone know how to implement this type of query with codeigniter's active record syntax?

3 Answer