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

Barbara Streisand
Release: 2024-10-23 01:01:03
Original
454 people have browsed it

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>
Copy after login

Error:

ERROR UPDATING CONTENT: SQLSTATE[HY000]: General error
Copy after login

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!

source:php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!