Home  >  Article  >  Database  >  How Do I Verify the Success of a MySQL DELETE Operation?

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 21:49:02221browse

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.

The above is the detailed content of How Do I Verify the Success of a MySQL DELETE Operation?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn