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

DDD
Release: 2024-10-22 23:50:29
Original
252 people have browsed it

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

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

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!

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
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!