What returns a successful MySQL DELETE? How to check success of DELETE?

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

Alternatively, do you know any other ways to verify the success of a DELETE? Or should I just make sure the string exists before I delete it? I am trying to avoid another request if possible.

+43
php mysql
May 28 '09 at 18:11
source share
3 answers

Assuming you are using mysql_query :

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

If you use PDO :: exec , this guide says the following:

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

I don't want to respond to snipe, but since this was chosen as the answer, I should note that mysql_query will return TRUE , even if the query does not actually delete anything. You must use mysql_affected_rows to verify this.

+53
May 28 '09 at 18:13
source share

Also, if you care about the number of rows affected:

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

+32
May 28 '09 at 18:18
source share

you can try this for your PHP code posted after running your request:

 if (mysql_affected_rows() > 0) { echo "You have successfully updated your data.<br><br>"; } else { echo "The data you submitted matched the current data so nothing was changed.<br><br>"; } 
+12
Sep 07 '10 at 1:57
source share



All Articles