Skip to content
adampatterson edited this page Sep 25, 2012 · 1 revision

A Simple UPDATE

Updating rows is very much like a SELECT query:

// Select the table
$table = db('mytable');

// Query the database
$table->update(array('email'=>'joemomma@gmail.com'))
      ->where('user','=','Joe')
      ->execute();

The above is the same as the following SQL:

UPDATE `mytable` SET `email`='joemomma@gmail.com' WHERE `user`='Joe'

A More Complex Example

We can add multiple WHERE conditions to our query easily as well as update multiple columns:

// Select the table
$table = db('mytable');

// Query the database
$table->update(array('email'=>'joemomma@gmail.com','name'=>'Joe Da Man'))
      ->where('user','=','Joe')
      ->clause('AND')
      ->where('email','=','joe@gmail.com')
      ->execute();

UPDATE mytable SET email='joemomma@gmail.com',name='Joe Da Man' WHERE user='Joe' AND email='joe@gmail.com'

Note that the values 'joemomma@gmail.com' and 'Joe Da Man' (in addtion to the values 'Joe' and 'joe@gmail.com') are automatically cleaned of any SQL Injection.