Home  >  Article  >  Database  >  How to Verify the Success of a MySQL DELETE Query in PHP?

How to Verify the Success of a MySQL DELETE Query in PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-11-01 05:52:27528browse

How to Verify the Success of a MySQL DELETE Query in PHP?

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:

  • mysql_query(): Returns TRUE if successful, FALSE otherwise.
  • PDO::exec(): Returns the number of rows affected, including 0 if no rows were removed.

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!

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