fetchAll() in the code, which shou"/> fetchAll() in the code, which shou">

Home  >  Article  >  Backend Development  >  Why Does PDO Show \"SQLSTATE[HY000]: General Error\" for Database Updates?

Why Does PDO Show \"SQLSTATE[HY000]: General Error\" for Database Updates?

DDD
DDDOriginal
2024-10-22 23:50:29253browse

Why Does PDO Show

PDO Error: "SQLSTATE[HY000]: General Error" When Updating Database

Despite your code successfully updating the database, you encounter an enigmatic error upon execution: "SQLSTATE[HY000]: General error."

Inspecting your code, we notice an unexpected inclusion:

<code class="php">$result = $stmt->fetchAll();</code>

This line of code is typically used to retrieve results from select statements, but in the context of an update query, it's incorrect. Specifically, fetchAll() should not be used for insert or update queries. Its removal should resolve the error.

Thus, your updated code would be:

<code class="php">try {
    $stmt = $pdo->prepare("UPDATE $page SET $section = :new_content WHERE $section = '$old_content'");
    $stmt->execute(array(
        'new_content' => $new_content
    ));
    echo "Database updated!";
}
catch(PDOException $e) {
    echo 'ERROR UPDATING CONTENT: ' . $e->getMessage();
}</code>

The above is the detailed content of Why Does PDO Show \"SQLSTATE[HY000]: General Error\" for Database Updates?. 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