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

前端 未结 4 721
天涯浪人
天涯浪人 2020-11-27 16:45

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\

相关标签:
4条回答
  • 2020-11-27 17:17

    you could try this for your php code, placed after your query runs:

    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>";
    }
    
    0 讨论(0)
  • 2020-11-27 17:28

    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.

    0 讨论(0)
  • 2020-11-27 17:29

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

    0 讨论(0)
  • 2020-11-27 17:40

    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.

    0 讨论(0)
提交回复
热议问题