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.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3