Home >Backend Development >PHP Tutorial >How to Check if a MySQL Query Successfully Modified Database Data in PHP?

How to Check if a MySQL Query Successfully Modified Database Data in PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-07 06:29:02292browse

How to Check if a MySQL Query Successfully Modified Database Data in PHP?

How to Verify the Success of a MySQL Query in Modifying Database Data

In PHP, you can utilize the affected_rows property of the statement object to determine whether a query successfully modified the database table data. Consider the following code snippet:

<code class="php">if($cmd=="deleterec"){
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
    if ($delRecord = $con->prepare($deleteQuery)) {
        $delRecord->bind_param("s", $pk);
        $delRecord->execute();
        $rowCount = $delRecord->affected_rows;
        $delRecord->close();
        echo ($rowCount > 0) ? "true" : "false";
    } else {
        echo "false";
    }
}</code>

In this code:

  1. After executing the DELETE statement, the affected_rows property holds the count of rows that were deleted.
  2. The ternary operator ($rowCount > 0) ? "true" : "false" checks if any rows were affected. If rowCount is greater than 0, it indicates a successful deletion.
  3. If the query execution failed or no rows were affected (rowCount is 0), the script returns "false" to the JavaScript function.

By using affected_rows, you can accurately determine the success or failure of the query in modifying the database table data.

The above is the detailed content of How to Check if a MySQL Query Successfully Modified Database Data 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