Home > Backend Development > PHP Tutorial > How to Verify the Success of MySQL Delete Queries in PHP?

How to Verify the Success of MySQL Delete Queries in PHP?

Linda Hamilton
Release: 2024-11-10 14:10:02
Original
206 people have browsed it

How to Verify the Success of MySQL Delete Queries in PHP?

Testing the Success of MySQL Queries in Modifying Database Tables

In crafting applications that interact with databases, it's critical to verify the successful execution of queries that alter data within tables. In PHP, checking the outcome of such queries can pose challenges. Consider the following code snippet:

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

This code aims to handle a deletion request and return "true" if successful. However, it solely checks whether the statement is prepared correctly, not whether the record was actually deleted. To rectify this, consider the following approach:

<code class="php">...
echo ($delRecord->affected_rows > 0) ? 'true' : 'false';
$delRecord->close();</code>
Copy after login

The affected_rows property returns the number of rows affected by the query. By checking if this value is greater than zero, you can accurately determine whether the record was successfully deleted.

Additionally, it's crucial to appropriately process the result string in your JavaScript code. If this is a source of trouble, providing more details about the JavaScript portion would enable a more comprehensive answer.

The above is the detailed content of How to Verify the Success of MySQL Delete Queries 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