2012-08-23 64 views
0

我目前正在PHP中創建一個頁面,將顯示一個用戶和他們的排名,在我的網站的高分。PHP的MySQLi多重查詢 - 命令不同步:mysqli_fetch_array

我有一個問題,我的MySQL查詢沒有充當預期,但是,這裏@VladBalmos解決我的問題:MySQL Highscores - User's Personal Ranks: Duplicate Entries Causing Incorrect Values

現在,我相信我的MySQL查詢是否正常工作,我需要解決的問題是與我的PHP代碼一起出現。這裏是我的代碼:

$userid = (int) $_GET['searched']; 
    for ($i = 0; $i < 5; $i++) { 
     if ($i == 0) { 
      $skill_query = mysqli_query($database, "SELECT uid, overall, overallxp, gamelevel FROM playerstats WHERE uid = ". $userid ." ORDER BY playerstats.overall DESC, playerstats.overallxp DESC") or print(mysqli_error($database)); 
      $skill_array = mysqli_fetch_array($skill_query); 
      //old rank query: $rank_query = mysqli_query($database, "SELECT count(*) + 1 FROM (SELECT uid, overall, overallxp, gamelevel FROM playerstats GROUP BY playerstats.uid) AS x WHERE overall > (SELECT overall FROM playerstats WHERE uid = ". $userid .")") or print(mysqli_error($database)); 
      $rank_query = mysqli_multi_query($database, "SET @rank=0; SELECT rank, uid, overall, overallxp FROM (SELECT @rank:[email protected] + 1 AS rank, uid, overall, overallxp FROM playerstats ORDER BY overall DESC, overallxp DESC) as tmp") or print(mysqli_error($database)); 
      $rank_array = mysqli_fetch_array($rank_query); 
     } else { 
      $skill_query = mysqli_query($database, "SELECT uid, ". $skills[$i] .", gamelevel FROM playerstats WHERE uid = ". $userid ." ORDER BY playerstats.". $skills[$i] ." DESC, playerstats.gamelevel DESC") or print(mysqli_error($database)); 
      $skill_array = mysqli_fetch_array($skill_query); 
      $rank_query = mysqli_query($database, "SELECT count(*) + 1 FROM (SELECT uid, ". $skills[$i] .", gamelevel FROM playerstats GROUP BY playerstats.uid) AS x WHERE ". $skills[$i] ." > (SELECT ". $skills[$i] ." FROM playerstats WHERE uid = ". $userid .")") or print(mysqli_error($database)); 
      $rank_array = mysqli_fetch_array($rank_query); 
     } 

     echo "<tr class='hs_row'>"; 
     echo "<td align='center'>"; 
     echo $rank_array[0]; //rank output 
     echo "</td>"; 
     echo "<td align='center'>"; 
     echo $skill_array[1]; //skilllevel 
     echo "</td>"; 
     echo "<td align='center'>"; 
     if ($i == 0) { 
      echo number_format($skill_array[2]); //skillxp 
     } else { 
      echo number_format($skill_array[1]); //skillxp 
     } 
     echo "</td></tr>"; 
    } 

...和我收到的錯誤是:

警告:mysqli_fetch_array()預計參數1被mysqli_result,在[路徑]定的boolean /個人。 php on line 156

命令不同步;不能運行此命令現在

警告:mysqli_fetch_array()預計參數1被mysqli_result,布爾在[路徑]給出/personal.php上線159

命令不同步;不能運行此命令現在

警告:mysqli_fetch_array()預計參數1被mysqli_result,布爾在[路徑]給出/personal.php上線161

命令不同步;你現在不能運行這個命令

所以,我的問題是,我該如何糾正這個錯誤?

回答

1
$rank_query = mysqli_multi_query($database, $yourQuery); 
if($rank_query) { 
    while(mysqli_next_result($dabatase)) { 
     if($result = mysqli_store_result($database)) { 
      while($row = mysqli_fetch_row($result) { 
       // do something 
      } 
     } 

    } 
} 

您必須遍歷多個查詢的結果,然後訪問每個結果集的行。由於第一個查詢只設置一個變量,你必須跳過設置下一個結果並獲取實際行(這就是爲什麼我做*而(mysqli_next_result ... *) 閱讀在php manual

+0

阿的文檔,我覺得當我嘗試它時,我忘記了mysqli_next_result。感謝您的幫助!:) – Mko