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
.
This will either return true or false
Check this for more information. Active Records
With this way you will also avoid sql injection that you had before
Use a stored procedure, you can check the result.
Below is a stored procedure example :
PHP example code :
As commented, have you tried
$this->db->affected_rows()
?That will tell you how many rows were updated.
I have use this code for checking update query.
A better solution I've found is to manage the difference between an ERROR and 0 affected rows. 0 affected rows is not necessarily a bad thing, but an error is something you do want to know about:
Now your function can differentiate...
Just some quick hacking there - you should obviously make the code far more verbose if you have other people using it.
The point is, consider using _error_message to differentiate between 0 updated rows and a real problem.