Home  >  Article  >  Backend Development  >  How to Verify MySQL Query Success in Database Modification?

How to Verify MySQL Query Success in Database Modification?

Susan Sarandon
Susan SarandonOriginal
2024-11-05 20:24:02214browse

How to Verify MySQL Query Success in Database Modification?

Verifying MySQL Query Success in Database Modification

To ascertain whether a MySQL query has successfully modified data in a table, it's crucial to verify both the query preparation and its execution. Here's the correct method to check the query's success in PHP:

<code class="php">
if ($delRecord = $con->prepare($deleteQuery)) {
    $delRecord->bind_param("s", $pk);
    $delRecord->execute();
    if ($delRecord->affected_rows > 0) {
        echo "true";
    } else {
        echo "false";
    }
    $delRecord->close();
} else {
    echo "false";
}
</code>

In this code, we additionally check $delRecord->affected_rows, which returns the number of rows impacted by the query. If the query executed successfully and at least one row was affected, the string "true" is returned. Otherwise, "false" is sent as the result.

Note that in your JavaScript code, you will need to appropriately handle the result string obtained from this PHP script to determine how the page should be updated via AJAX.

The above is the detailed content of How to Verify MySQL Query Success in Database Modification?. 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