我很擅长使用预处理语句,并且想知道我是否应该在每次调用后关闭stmt_init(),还是可以让它保持打开状态?
$stmt = $mysqli->stmt_init();
if($stmt->prepare("SELECT player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ? AND fk_player_id = ?")){
$stmt->bind_param('ii', $currgame, $playerid);
$stmt->execute();
$stmt->bind_result($udraws, $uturn, $upass, $uswaps);
$stmt->fetch();
echo $udraws.'-'.$uturn.'-'.$upass.'-'.$uswaps.'<br>';
// Close statement object
$stmt->close();
}
$stmt = $mysqli->stmt_init();
if($stmt->prepare("SELECT player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ? AND fk_player_id != ?")){
$stmt->bind_param('ii', $currgame, $playerid);
$stmt->execute();
$stmt->bind_result($odraws, $oturn, $opass, $oswaps);
$stmt->fetch();
echo $odraws.'-'.$oturn.'-'.$opass.'-'.$oswaps.'<br>';
// Close statement object
$stmt->close();
}
考虑到呼叫,其中一个对数据库更好吗?
提前致谢!
答案 0 :(得分:1)
...准备好的语句或参数化语句用于以高效率重复执行相同的语句...
修改强> 由于查询不同,每个查询都需要单独准备,但您应该能够重用$ mysqli-&gt; stmt_init();
另一方面,有人在comments中提到了这一点:
*如果你在循环中使用bind_param重复一个语句,依此类推,以便进行更大的操作。我认为用stmt-&gt;关闭来清理它会很好。但它在aprox后总是出现错误。 250次操作。当我尝试使用stmt-&gt;重置它对我有用。*