Home  >  Article  >  Backend Development  >  Why is fetchAll() causing a \"SQLSTATE[HY000]: General Error\" in PDO when updating a database?

Why is fetchAll() causing a \"SQLSTATE[HY000]: General Error\" in PDO when updating a database?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-23 00:53:30952browse

Why is fetchAll() causing a

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

Despite the error message, your code appears to be updating the database successfully. The issue lies with the use of fetchAll() after an update or insert query.

Understanding the Error

The error message "SQLSTATE[HY000]: General error" provides little information about the actual problem. It usually indicates a generic issue with the database query.

Identifying the Problem

In your code, after executing the update query, you are using fetchAll() to retrieve the results. This method is typically used with select queries, where it retrieves all the rows returned by the query. However, for insert or update queries, fetchAll() is unnecessary and can cause errors.

Solution

To resolve the error, simply remove the fetchAll() statement:

<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 is fetchAll() causing a \"SQLSTATE[HY000]: General Error\" in PDO when updating a database?. 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