"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How Do I Verify the Success of a MySQL DELETE Operation?

How Do I Verify the Success of a MySQL DELETE Operation?

Published on 2024-11-08
Browse:356

How Do I Verify the Success of a MySQL DELETE Operation?

How to Verify the Success of a MySQL DELETE Operation

When executing a DELETE query using MySQL, it's crucial to determine whether the operation was successful. This is particularly important when deleting specific records based on criteria.

mysql_query() Method:

For traditional MySQL interactions using mysql_query(), a successful DELETE operation returns a boolean value:

  • TRUE: The deletion was successful.
  • FALSE: The deletion failed or no records were affected.

PDO::exec() Method:

With PHP Data Objects (PDO), PDO::exec() is used for DELETE statements. Unlike mysql_query(), PDO::exec() returns the number of affected rows:

  • 0: No records were affected or deleted.
  • >0: The specified number of rows were deleted.

Additional Tips:

  • To cater for edge cases where mysql_query() might return TRUE despite no rows being affected, consider using mysql_affected_rows() to explicity check how many rows were deleted.
  • For greater efficiency, check if the row exists before attempting to delete it to avoid unnecessary database queries.
Latest tutorial More>

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