Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php PDO错误-PDO异常,消息为“SQLSTATE[HY000]:一般错误”_Php_Mysql_Pdo - Fatal编程技术网

Php PDO错误-PDO异常,消息为“SQLSTATE[HY000]:一般错误”

Php PDO错误-PDO异常,消息为“SQLSTATE[HY000]:一般错误”,php,mysql,pdo,Php,Mysql,Pdo,我得到这个错误: 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=:

我得到这个错误:

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;
}
我不知道为什么会这样。我已经检查了查询,但根本找不到任何错误。

这就是发生的情况:

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

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

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

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


对于像我一样来这里试图破译这条深奥错误信息的其他人,让我补充一点:

尝试在pdo语句上运行回迁,如:

$statement->fetchAll();

…在INSERT或UPDATE**语句之后,由于没有数据可获取,因此可能会导致此错误


**更新。。。返回。。。语句是一个例外。

我认为应该从查询$userdata['username']的开始处进行try{}catch,您确定这是合法的吗?调用$f=$s->fetchAll;而不是取回。可能返回了多行,而您在准备另一条语句之前还没有获取所有行。这可能是MySQL中神秘错误的来源。等等,count$s->fetchAll的目的是什么?此时,$s是准备好的更新查询,而不是选择查询。那么执行更新查询的正确方法是什么?我的更新查询被构建并存储在变量中,然后像这样执行:$results=$readConnection->fetchAll$query@deeperDATA的要点是,您应该延迟插入/更新/删除,直到完成获取结果为止。因此,您不必选择、更新、获取所有更新,而是选择、将更新存储在临时变量、获取所有更新、从临时变量更新这与您尝试从更新或插入获取数据的位置/时间无关。更新和插入根本不需要提供获取数据!它们确实作用于一定数量的行,因此$statement->rowCount的元数据是可用的,但是没有像select这样的获取数据…我在像USE DBNAME这样的单个语句中执行多个查询时也出现了这个错误;选择
$statement->fetchAll(PDO::FETCH_ASSOC);