Home  >  Article  >  Backend Development  >  Why Do I Get \"PDO Error: \'SQLSTATE[HY000]: General Error\'\" When Updating a Database with PDO?

Why Do I Get \"PDO Error: \'SQLSTATE[HY000]: General Error\'\" When Updating a Database with PDO?

Barbara Streisand
Barbara StreisandOriginal
2024-10-23 01:01:03455browse

Why Do I Get

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

In PDO, updating a database can occasionally trigger the disconcerting error message: "SQLSTATE[HY000]: General error." However, the peculiar aspect of this error is that the database update is often successful despite the reported issue.

Code:

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

Error:

ERROR UPDATING CONTENT: SQLSTATE[HY000]: General error

Solution:

The root of this issue lies in the use of the fetchAll() method after executing an update or insert query. This method should not be used in such scenarios, as it attempts to retrieve data from the database, which is not applicable to update or insert operations. Removing the $result = $stmt->fetchAll(); line should resolve the issue.

The above is the detailed content of Why Do I Get \"PDO Error: \'SQLSTATE[HY000]: General Error\'\" When Updating a Database with PDO?. 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