phpcodeignitersql-updatelimitquery-builder

Calling CodeIgniter's limit() after update() does not include a LIMIT clause in the executed query


I'm trying to update a single record with CodeIgniter's query builder methods, but it is not working as intended.

$data = array('status' => 1);
$this->db->where('field1', $info);
$this->db->update('example', $data);
$this->db->limit(1);

When I run this, it updates all qualifying records.

Printing $this->db->last_query() shows that there is no LIMIT declaration in the executed query.

What is the error?


Solution

  • Move the limit before the update which should be always called last:

    $this->db->limit(1);
    $this->db->update('example', $data);