Verifying the Success of a MySQL DELETE Query
When performing a DELETE operation, it's crucial to ascertain its successful execution. In PHP, you can employ various methods to determine if the DELETE query was successful.
MySQLi and PDO
Using MySQLi or PDO, mysql_query() and PDO::exec() return different values upon successful DELETE queries:
Using mysql_affected_rows()
To ensure that rows were actually removed, use mysql_affected_rows(). If it returns a value greater than 0, the query was successful. However, note that mysql_affected_rows() is not supported in all cases, such as when using the LOW_PRIORITY option.
Checking for Row Existence Before Deletion
Alternatively, to prevent unnecessary queries, you can check if the row exists before attempting to delete it. Use a SELECT query to verify the row's presence. If the row exists, proceed with the DELETE operation; otherwise, skip it.
The above is the detailed content of How to Verify the Success of a MySQL DELETE Query in PHP?. For more information, please follow other related articles on the PHP Chinese website!