2013-02-03 66 views
3

我對準備好的語句很陌生,我不確定我是這樣做的。用準備好的語句獲取多行

這裏是我的嘗試:

$currgame = 310791; 

$sql = "SELECT fk_player_id, player_tiles, player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ?"; 
$stmt = $mysqli->stmt_init(); 

$data = array(); 
if($stmt->prepare($sql)){ 
    $stmt->bind_param('i', $currgame); 
    $stmt->execute(); 

    $fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null; 
    $stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped); 

    $res = $stmt->get_result(); 

    while ($row = $res->fetch_assoc()){ 
     $data[] = $row; 
    } 
    $stmt->close(); 
} 

// to display own games 
foreach ($data as $row) { 
    if ($row['fk_player_id'] == $playerid) { 

     $udraws = $row['player_draws']+1; 
     $upass = $row['player_passes']; 
     $uswaps = $row['swapped']; 

     echo 'uDraws: '.$udraws.'<br>'; 
     echo 'uPass: '.$upass.'<br>'; 
     echo 'uSwaps: '.$uswaps.'<br><br>'; 
    } 
} 
// to display other games 
foreach ($data as $row) { 
    if ($row['fk_player_id'] != $playerid) { 

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

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

     echo 'oID: '.$opponent.'<br>'; 
     echo 'oTiles: '.$oppTiles.'<br>'; 

     echo 'oDraws: '.$odraws.'<br>'; 
     echo 'oPass: '.$opass.'<br>'; 
     echo 'oSwaps: '.$oswaps.'<br><br>'; 

    } 
} 

我得到一個 「SERVERERROR」 試圖運行這個時:這是 「$解析度= $ stmt-> get_result();」這使得錯誤,但不知道爲什麼。請幫忙。

在此先感謝:-)

#### EDIT
$sql = "SELECT fk_player_id, player_tiles, player_draws, player_turn, player_passes, swapped FROM ".$prefix."_gameplayer WHERE fk_game_id = ?"; 
$stmt = $mysqli->stmt_init(); 

$data = array(); 
if($stmt->prepare($sql)){ 
    $stmt->bind_param('i', $currgame); 
    $stmt->execute(); 

    $fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null; 
    $stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped); 

    while ($row = $stmt->fetch()){ 
     $data[] = $row; 
    } 
    $stmt->close(); 
} 

echo '<pre>'; 
print_r($data); 
echo '</pre>'; 
+0

請詳細說明 「SERVERERROR」。任何指示錯誤來自哪裏(MySQL,Apache,PHP)?任何不尋常的HTTP狀態代碼?什麼是完整的錯誤信息? – Oswald

回答

11

根據您的PHP/MySQL設置,您可能無法使用get_result()。

解決此問題的方法是綁定結果。

例如:

$stmt->execute(); 

$fk_player_id = null; $player_tiles = null; $player_draws = null; $player_turn = null; $player_passes = null; $swapped = null; 

$stmt->bind_result($fk_player_id, $player_tiles, $player_draws, $player_turn, $player_passes, $swapped); 

while ($stmt->fetch()) { // For each row 
    /* You can then use the variables declared above, which will have the 
    new values from the query every time $stmt->execute() is ran.*/ 
} 

For more information click here

+0

嘗試了這一點(請參閱原文中的編輯),但我仍然收到錯誤:(HTTP錯誤500(內部服務器錯誤))。如果我在「$ res = $ stmt-> get_result();」之前退出該網頁加載罰款?不知道什麼是錯的? – Mansa

+0

@Mansa嗯。你有沒有訪問服務器日誌? –

+0

不能訪問: -/ – Mansa

0

由於我沒有看到它在你的代碼,確保你嘗試在它之前查詢正在實例化對象mysqli

$mysqli = new mysqli("127.0.0.1", "user", "password", "mydb"); 
if($mysqli->connect_error){ 
    die("$mysqli->connect_errno: $mysqli->connect_error"); 
} 

此外,ServerError肯定會在日誌中顯示出來,併爲您指出正確的方向。