問題描述
我收到此錯誤:
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error' in ...
...每當我使用 PDO 執行此代碼時:
..whenever I execute this code with 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..
推薦答案
事情是這樣的:
您正在嘗試獲取 UPDATE 查詢.您不能這樣做,因為 UPDATE 查詢不返回值.如果您想知道查詢影響了多少行,請改用 rowCount() 函數.請注意,并非所有數據庫驅動程序都提供受影響的行.
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
另外,建議在try塊內進行所有PDO操作,否則可能會得到未處理的異常.
Also, it is recommended to make all PDO operations within the try block, otherwise you may get unhandled exceptions.
這篇關于PDO 錯誤 - PDOException' 帶有消息 'SQLSTATE[HY000]: 一般錯誤'的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!