Home > Database > Mysql Tutorial > How Can I Reliably Confirm MySQL Query Success After Modifying Data?

How Can I Reliably Confirm MySQL Query Success After Modifying Data?

Mary-Kate Olsen
Release: 2024-12-01 08:16:09
Original
238 people have browsed it

How Can I Reliably Confirm MySQL Query Success After Modifying Data?

Confirming MySQL Query Success: Modifying Database Table Data

Issue:

When executing a MySQL query that modifies database table data, it's essential to ascertain its success. This ensures proper handling of actions dependent on the query's outcome.

Proposed Solution:

The provided PHP code attempts to delete a database record but only checks if the query is prepared correctly. It doesn't verify whether the record was successfully removed.

To accurately determine query success, the following steps should be taken:

  1. Prepare and execute the SQL statement as usual.
  2. Check the affected_rows property of the query result. If it is greater than 0, the query successfully modified data.

Revised Code:

if ($cmd == "deleterec") {
    $deleteQuery = "DELETE FROM AUCTIONS1 WHERE ARTICLE_NO = ?";
    if ($delRecord = $con->prepare($deleteQuery)) {
        $delRecord->bind_param("s", $pk);
        $delRecord->execute();
        $rowsAffected = $delRecord->affected_rows;
        $delRecord->close();
        echo ($rowsAffected > 0) ? 'true' : 'false';
    } else {
        echo "false";
    }
}
Copy after login

Additional Considerations:

It's important to also verify the result interpretation in the JavaScript code. If there is an issue there, it may not correctly handle the 'false' response from the PHP script.

The above is the detailed content of How Can I Reliably Confirm MySQL Query Success After Modifying Data?. 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