I have a function to delete a row from my table and I like to return true if the row existed before. I use the following function achieve that:
$mysqli = $this->mysqli;
$stmt = $mysqli->prepare("DELETE FROM active_clients WHERE token LIKE ?");
$stmt->bind_param('s', $token);
$stmt->execute();
if($stmt->affected_rows < 1){
$stmt->close();
return false;
}
$stmt->close();
return true;
It also worked propperly on my local host but when I moved to live server it doesn't return the correct result. affected_rows returns always 0 but the row is deleted as it should be. How can it be? Could there be a problem with php-version? I run locally 5.4 and on live server 5.3... Or ist it also possible that it has something to do with database or server configuration?
Try this:
$mysqli = $this->mysqli;
$stmt = $this->mysqli->prepare("DELETE FROM active_clients WHERE token LIKE ?");
if(!$stmt){
die('prepare() failed: ' . $mysqli->error);
}else{
$stmt->bind_param('s', $token);
if($stmt->execute()){
$stmt->store_result();
if($stmt->affected_rows < 0){
$stmt->close();
return false;
}else{
$stmt->close();
return true;
}
}else{
die('execute() failed: ' . $mysqli->error);
}
}
If the last query was a DELETE query with no WHERE clause, all of the records will have been deleted from the table but this function will return zero with MySQL versions prior to 4.1.2.
Check your Mysql version.
Resource: http://php.net//manual/en/function.mysql-affected-rows.php