What does a successful MySQL DELETE return? How to check if DELETE was successful?


Using PHP, I am trying to delete a record, but I want to check if it was successful or not. Is anything returned from a successful DELETE FROM foo where bar = 'stuff'?

Alternatively, do you know any other ways to check if a DELETE was successful? Or am I better off just making sure the row exists before I delete it? I am trying to avoid another query if possible.

3/25/2011 1:24:17 PM

Accepted Answer

Assuming you are using mysql_query:

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

If you are using PDO::exec, then the manual says this:

PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec() returns 0.

Don't want to answer snipe, but since this was selected as the answer, I should note that mysql_query will return TRUE even if the query did not actually remove anything. You should use mysql_affected_rows to check for that.

5/28/2009 6:51:35 PM

Additionally, if you care about the number of rows that were affected:

[Use] mysql_affected_rows() to find out how many rows were affected by a DELETE, INSERT, REPLACE, or UPDATE statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow