Home > Database > Mysql Tutorial > body text

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

Barbara Streisand
Release: 2024-11-01 05:52:27
Original
527 people have browsed it

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!