我最近切换到PDO,在这里有许多精彩的建议之后使用预备语句。我试图转换一个简单的视图计数器用于PDO,但我很难让它实际更新表。你们是否会介意我在这里做错了什么?谢谢!
旧工作代码:
//Update view counter
$old_views = $postCrumb['topic_views'];
$new_views = $old_views + 1;
$qviewCount = "UPDATE topics SET topic_views='".$new_views."' WHERE cat_id='".$cidClean."' AND id='".$tidClean."' LIMIT 1";
$viewCount = mysql_query($qviewCount) or die(mysql_error());
新的非工作守则:
//Update view counter
$old_views = $postCrumb['topic_views'];
$new_views = $old_views + 1;
$qviewCount = "UPDATE topics SET topic_views='".$new_views."' WHERE cat_id=? AND id=? LIMIT 1";
$viewCount = $conn->prepare($qViewCount);
$viewCount -> execute(array($cidClean, $tidClean));
我也试过标记条目,但这也不起作用:
$qviewCount = "UPDATE topics SET topic_views='".$new_views."' WHERE cat_id=':cat_id' AND id=':topic_id' LIMIT 1";
$viewCount = $conn->prepare($qViewCount);
$viewCount -> execute(array(':topic_id'=>$tidClean,':cat_id'=>$cidClean));
答案 0 :(得分:0)
您声明$qviewCount
(小'V')的变量和传递给PDO::Prepare
$qViewCount
(大写'V')的变量不一样。因此,在告诉您查询为空时错误是正确的(因为您从未将$qViewCount
(使用大写'V')初始化为值。