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!