-
Notifications
You must be signed in to change notification settings - Fork 3
Update
Adam Patterson edited this page Dec 18, 2012
·
1 revision
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'
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.