我可以在同一个预备语句中从等于更改为不等于

时间:2013-02-02 14:25:39

标签: php mysqli prepared-statement

我想弄清楚如何创建预准备语句。我现在有这个:

$stmt = $mysqli->stmt_init();

if($stmt->prepare("SELECT fk_player_id, 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($playerid, $udraws, $uturn, $upass, $uswaps);
    $stmt->fetch();

    // HERE I WANT TO MAKE A NEW CALL //

    $stmt->close(); 

}

区域//这里我想打个新电话// 我想运行这个:

$sql = mysql_query("SELECT fk_player_id, player_tiles, player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = $currgame AND fk_player_id != $playerid");
$row = mysql_fetch_assoc($sql);

$opponent = $row['fk_player_id'];
$oppTiles = $row['player_tiles'];

$odraws = $row['player_draws']+1;
$oturn = $row['player_turn'];
$opass = $row['player_passes'];
$oswaps = $row['swapped'];

除了=和!=之外,这两个电话是正确的。这可以在同一个声明中完成吗?

提前致谢: - )

1 个答案:

答案 0 :(得分:1)

据我了解你的问题,你需要所有球员 好吧,全部选择它们。您可以在展示时间分开

$sql = "SELECT * FROM {$prefix}_gameplayer WHERE fk_game_id = ?";
$stmt = $mysqli->stmt_init();

$data = array();
if($stmt->prepare($sql)){
    $stmt->bind_param('i', $currgame);
    $stmt->execute();
    $res = $stmt->get_result();
    while ($ow = $res->fetch_assoc()) {
        $data[] = $row;
    }
    $stmt->close(); 
}
// to display own games
foreach ($data as $row) {
    if ($row['fk_player_id'] == $playerid) {
        // do something
    }
}
// to display other games
foreach ($data as $row) {
    if ($row['fk_player_id'] != $playerid) {
        // do something
    }
}