Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

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.

like image 250
jergason Avatar asked May 28 '09 18:05

jergason


People also ask

Does delete return any value SQL?

The standard DELETE statement in SQL returns the number of deleted rows. In PostgreSQL you can make DELETE statement return something else. You can return all rows that have been deleted. You can return the columns of your choice.

How can I count deleted records in MySQL?

You can use ROW_COUNT() function to check the number of deleted rows. The conditions in the WHERE clause (optional) identify which rows to delete. Without WHERE clause, all rows are deleted. If you specify the ORDER BY clause, the rows are deleted in specified order.

Which MySQL statement is used to delete data from a database *?

The MySQL DELETE Statement The DELETE statement is used to delete existing records in a table.


1 Answers

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.

like image 77
Paolo Bergantino Avatar answered Sep 20 '22 13:09

Paolo Bergantino