由于我不知道如何启动存储过程,所以我只是在几个查询中将其写出来,但我想没关系。
$STH = $DBH->prepare("SELECT logo_id, guess_count, guessed, guess FROM guess WHERE user_id=:id GROUP BY logo_id");
$STH->bindParam(":id",$loginuser['id']);
$STH->execute();
while($row = $STH->fetch()){
if($row['guessed'] == 0){
$STH2 = $DBH->prepare("SELECT guess, guess_count, guessed FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH2->bindParam(":logo_id",$row['logo_id'],PDO::PARAM_STR,20);
$STH2->bindParam(':id',$_SESSION['login'],PDO::PARAM_INT);
$STH2->execute();
$row2 = $STH2->fetch(PDO::FETCH_ASSOC);
if($row2){
$STH3 = $DBH->prepare("UPDATE guess SET guess_count=guess_count+:guess_count, guessed=:guessed, guess=:guess WHERE logo_id=:logo_id AND user_id=:id");
$data = array('guess_count'=>$row2['guess_count'],'guessed'=>$row2['guessed'],'guess'=>$row2['guess'],'logo_id'=>$row['logo_id'],'id'=>$loginuser['id']);
$STH3->execute($data);
$STH3 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH3->bindParam(":logo_id",$row['logo_id']);
$STH3->bindParam(':id',$_SESSION['login']);
$STH3->execute();
}
}else{
$STH2 = $DBH->prepare("DELETE FROM guess WHERE logo_id=:logo_id AND user_id=:id");
$STH2->bindParam(":logo_id",$row['logo_id']);
$STH2->bindParam(':id',$_SESSION['login']);
$STH2->execute();
}
}
$STH = $DBH->prepare("UPDATE guess SET user_id=:login WHERE user_id=:session"); // update all entries that are new from session
$STH->bindParam(':login',$loginuser['id']);
$STH->bindParam(':session',$_SESSION['login']);
$STH->execute();