Reputation: 31
Hey hello everyone,i have a slight problem with my small delete function in php,below is my code
function delete()
{
$q = "DELETE FROM example WHERE **author='frank'";**
$r = mysql_query($q) or die (mysql_error());
if($r)
{
echo 'done';
}
else
{
echo 'not done';
}
}
Now i don't have any author with that name Frank so that means it is not deleting anything from the database but still shows that done msg
I am not sure why????can anyone please help me
Upvotes: 3
Views: 231
Reputation: 21483
There is no error in your query. It will complete successfully. If you read the documentation you will see:
"For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error."
As there is no error in your query it will return TRUE
even though nothing has actually been deleted. Deleting nothing is not considered an error.
Upvotes: 1
Reputation: 60559
A delete query will always succeed, even if no rows are actually deleted. If you want to determine how many rows were affected by an insert, update or delete operation, use mysql_affected_rows()
Upvotes: 0
Reputation: 70587
That's because there was no error, delete did execute, it just didn't do anything. You want:
if(mysql_affected_rows() > 0) {
echo "done";
}
Upvotes: 5