php PDO 错误 - PDOException' 带有消息 'SQLSTATE[HY000]: 一般错误'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20315898/
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 03:04:27  来源:igfitidea点击:

PDO Error - PDOException' with message 'SQLSTATE[HY000]: General error'

phpmysqlpdo

提问by oliverbj

I am getting this error:

我收到此错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in ...

..whenever I execute this code with PDO:

..每当我使用 PDO 执行此代码时:

//Select data from the topic.
$s = $dbh->prepare("SELECT * FROM forum_topics WHERE forum_id=:forum_cat_id AND topic_id=:topicid");
$s->bindParam(':forum_cat_id', $forum_cat_id);
$s->bindParam(':topicid', $topicid);
$s->execute();
$f = $s->fetch();

$s = $dbh->prepare("UPDATE forum_cats 
    SET 
        forum_last_postid = :last_post_id, forum_last_posttime = :time, 
        forum_last_userid = :userid, forum_last_username = :username, 
        forum_posts=forum_posts+1 
    WHERE forum_id = :forum_cat_id");
$s->bindParam(':last_post_id', $last_post_id);
$s->bindParam(':time', $time);
$s->bindParam(':userid', $userid);
$s->bindParam(':username', $userdata['username']);
$s->bindParam(':forum_cat_id', $forum_cat_id);
try {
    $s->execute();
}
catch(PDOException $e) {
    die($e->getMessage());
}

if (count($s->fetchAll()) == 0) {
    return 3;
}

I have no idea why this is happening. I've checked the query, and I simply cant find any errors..

我不知道为什么会这样。我检查了查询,我根本找不到任何错误..

回答by Byte Bit

This is what happens:

这是发生的事情:

  • You are trying to fetch an UPDATE query. You can't do that because UPDATE queries does not return values. If you wish to know how many rows were affected by the query, use the rowCount() function instead. Notice that not all DB Drivers provide the affected rows.

  • You are using undeclared variables (at least in the code you posted here). This isn't the reason for this particular error, but could generate others.

  • You're not using the data you have selected from the database

    Also, it is recommended to make all PDO operations within the try block, otherwise you may get unhandled exceptions.

  • 您正在尝试获取 UPDATE 查询。您不能这样做,因为 UPDATE 查询不返回值。如果您想知道查询影响了多少行,请改用 rowCount() 函数。请注意,并非所有数据库驱动程序都提供受影响的行。

  • 您正在使用未声明的变量(至少在您在这里发布的代码中)。这不是此特定错误的原因,但可能会产生其他错误。

  • 您没有使用从数据库中选择的数据

    此外,建议在 try 块内进行所有 PDO 操作,否则您可能会遇到未处理的异常。

回答by Kzqai

For others who came here trying to decipher this esoteric error message as I did, let me add that:

对于那些像我一样来到这里试图破译这个深奥的错误信息的人,让我补充一下:

Trying to run fetches on pdo statements like:

尝试在 pdo 语句上运行提取,例如:

$statement->fetchAll();

or

或者

$statement->fetchAll(PDO::FETCH_ASSOC);

...after an INSERTor UPDATE** statement can cause this error (since there is no data to fetch).

...在INSERTUPDATE** 语句之后可能会导致此错误(因为没有要获取的数据)。

**The UPDATE... RETURNING... statement is an exception to this.

**UPDATE... RETURNING...语句是一个例外。