2013-02-02 62 views
0

我想知道如何創建預準備語句。我現在有這樣的:我可以在相同的準備語句中將等於或不等於

$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']; 

汀是taht 2個電話是除了=和!=以外。這可以在同一個陳述中完成嗎?

在此先感謝:-)

+1

沒有使用相同的語句,但如果這是在試圖減少額外的數據庫查詢,你可以只得到所有的結果在一個(離開了'共fk_player_id')聲明並手動分割結果。 – RayViljoen

+0

爲什麼你在第二個代碼部分使用'mysql_ *'函數?它們已被棄用,並且您沒有正確地轉義查詢參數。 –

回答

0

據我瞭解你的問題,你需要所有的球員。
好吧,全部選中。您可以在顯示時分開

$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 
    } 
} 
+0

嘿,看起來像正確的解決方案,除了...我試圖運行這個時,得到一個服務器錯誤頁面。錯誤從這裏開始:$ res = $ stmt-> get_result();有任何想法嗎? – Mansa

+0

get_result不是每個構建都支持。所以你必須像以前那樣得到你的數據。 –

+0

對不起,新來準備...你失去了我。然後我不能使用上面的exapmle? – Mansa