PDO错误:“SQLSTATE[HY000]:通用错误”更新数据库时

时间:2022-01-13 07:41:25

I am getting an error when updating a database using PDO. I am new to PDO so maybe the problem is a small one and I just don't understand. Funny thing about the error, the command works fine and the database does actually get updated. But it still returns an error back at me.

我在使用PDO更新数据库时出错。我对PDO不熟,所以问题可能很小,我就是不明白。有趣的是,这个错误,命令运行良好,数据库确实得到了更新。但是它仍然返回一个错误给我。

Code:

代码:

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

Error: ERROR UPDATING CONTENT: SQLSTATE[HY000]: General error

错误:错误更新内容:SQLSTATE[HY000]:一般错误

I literally have no idea where the problem could be because its very vaque and I haven't been able to find anyone with the same problem.

我真的不知道问题会在哪里,因为问题就在眼前,我找不到任何有同样问题的人。

2 个解决方案

#1


81  

You do not use fetchAll(),as in

您不使用fetchAll(),如in。

$result = $stmt->fetchAll();

with update or insert queries. Removing this statement should rectify the problem.

使用更新或插入查询。删除这一声明应该纠正这个问题。

#2


9  

Just to note, another possible reason for this error is if you make a second database call with the variable $stmt inside of an existing parent $stmt loop.

需要注意的是,这个错误的另一个可能的原因是,如果您在一个现有的父$stmt循环中使用变量$stmt进行第二次数据库调用。

     $stmt = $conn->query($sql);

    while ($row = $stmt->fetch()) {  //second use of $stmt here inside loop

#1


81  

You do not use fetchAll(),as in

您不使用fetchAll(),如in。

$result = $stmt->fetchAll();

with update or insert queries. Removing this statement should rectify the problem.

使用更新或插入查询。删除这一声明应该纠正这个问题。

#2


9  

Just to note, another possible reason for this error is if you make a second database call with the variable $stmt inside of an existing parent $stmt loop.

需要注意的是,这个错误的另一个可能的原因是,如果您在一个现有的父$stmt循环中使用变量$stmt进行第二次数据库调用。

     $stmt = $conn->query($sql);

    while ($row = $stmt->fetch()) {  //second use of $stmt here inside loop