gpt4 book ai didi

php - PDO 错误 - PDOException',消息为 'SQLSTATE[HY000]: General error'

转载 作者:IT王子 更新时间:2023-10-29 00:30:04 25 4
gpt4 key购买 nike

我收到这个错误:

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

..每当我用 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;
}

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

最佳答案

是这样的:

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

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

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

    此外,建议在 try block 内进行所有 PDO 操作,否则可能会出现未处理的异常。

关于php - PDO 错误 - PDOException',消息为 'SQLSTATE[HY000]: General error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20315898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com