php – PDO错误 – PDOException’,带有消息’SQLSTATE [HY000]:一般错误’

参见英文答案 > PDO error: “ SQLSTATE[HY000]: General error ” When updating database                                    2个
我收到此错误

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()函数.请注意,并非所有DB驱动程序都提供受影响的行.
>您正在使用未声明的变量(至少在您在此处发布的代码中).这不是导致此特定错误的原因,但可能会产生其他错误.
>您没有使用从数据库中选择的数据

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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...