2

我对使用准备好的语句很陌生,想知道是否应该在每次调用后关闭 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(); 

}

考虑到调用,其中一个对数据库更好吗?

提前致谢!

4

1 回答 1

1

...准备语句或参数化语句用于高效地重复执行同一语句...

编辑 由于查询不同,因此每个查询都需要单独准备,但您应该能够重用 $mysqli->stmt_init();

在旁注中,有人在评论中提到了这一点:

*如果您在循环中使用 bind_param 等重复语句以进行更大的操作。我认为用 stmt->close 清理它会很好。但它总是在 aprox 之后出现错误。250 次操作。当我尝试使用 stmt->reset 时,它对我有用。*

于 2013-01-17T07:51:51.573 回答