I have a model function that updates a user in my CodeIgniter application:
// updates first of a user, return true if successful, false if not.
public function updateFirstName($userId, $newFirstName) {
$this->db->query("UPDATE users SET firstName='$newFirstName' WHERE id=$userId");
return // whether request was successful?
}
How do I return a boolean value that ensures the user of ID $userId
has been updated? For instance, it should return false if no user was found with ID $userId
.
You may use
$this->db->affected_rows();
to check whether query runs successfully or notYou can use
$this->db->affected_rows()
in Codeigniter this returns a numeric value when doing "write" type queries (insert, update, etc.).In MySQL
DELETE FROM TABLE
returns 0 affected rows. The database class has a small hack that allows it to return the correct number of affected rows. By default this hack is enabled but it can be turned off in the database driver file. (From CI user guide). For deleted row in Ci it returns 1.or
or
EDIT
also(much better)
Try this: