3

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

2
  • Needs more info. What does your example table contain? What data type is the author column? Commented May 16, 2011 at 16:20
  • mysql_query() returns a STATEMENT HANDLE if the query succeeded, or FALSE if there was an error. A query which does nothing is still considered a successful query, so unless your query has a syntax error or some other database error occurs, you will always get the handle. Commented May 16, 2011 at 16:23

3 Answers 3

5

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";
}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.