John Hoffman John Hoffman - 2 months ago 6
MySQL Question

How do I test if my MySQL update query was successful in Code Igniter?

I have a model function that updates a user in my Code Igniter 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
.

Answer

As commented, have you tried $this->db->affected_rows()?

That will tell you how many rows were updated.

Comments