php PDO 错误:“SQLSTATE[HY000]:一般错误”更新数据库时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12979510/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:35:09  来源:igfitidea点击:

PDO error: " SQLSTATE[HY000]: General error " When updating database

phppdo

提问by loxyboi

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.

我真的不知道问题出在哪里,因为它非常空洞,而且我找不到任何有同样问题的人。

回答by TranQ

You do not use fetchAll(),as in

您不使用 fetchAll(),如

$result = $stmt->fetchAll();

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

带有更新或插入查询。删除此语句应该可以解决问题。

回答by Acyra

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