2013-12-09 65 views
2

傢伙我在這裏和新MySQL的新太..創建排名基本系統和更新排名

所以我想創建用於管理團隊record.The數據庫的數據庫包含了一個名爲團隊表一中設置如下列,

  • TeamID
  • TeamRank
  • TeamName
  • TeamWins
  • TeamLoss
  • TeamPoints

所以議程是排名積分的基礎上隊,積分越大越高等級。

<?php 
$con = mysqli_connect("", "", "", ""); 
// Check connection 
if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result = mysqli_query($con, "SET @rownum := 0; 
    INSERT INTO team(TeamRank, TeamName) 
    SELECT @rownum := @rownum + 1 AS TeamRank, TeamName 
    FROM (SELECT SUM(TeamRank)AS TeamRank , TeamName 
    FROM team 
    GROUP BY TeamName 
    ORDER BY TeamRank DESC) as result 
    ON DUPLICATE KEY UPDATE TeamName = VALUES(TeamName);" 
); 

echo "<table border='1'> 
    <tr> 
    <th>Rank</th> 
    <th>TeamID</th> 
    <th>TeamName</th> 
    <th>Total Points</th> 
    </tr>"; 

while ($row = mysqli_fetch_array($result)) { 
    echo "<tr>"; 
    echo "<td>" . $row['TeamRank'] . "</td>"; 
    echo "<td>" . $row['TeamID'] . "</td>"; 
    echo "<td>" . $row['TeamName'] . "</td>"; 
    echo "<td>" . $row['TeamPoints'] . "</td>"; 
    echo "</tr>"; 
} 

echo "</table>"; 

mysqli_close($con); 

林recieveing這個錯誤

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result 

我在哪裏錯了?如果代碼存在問題,請指導我。

PS [編輯]:我想要做的就是以TeamName,TeamWins,TeamLoss和TeamPoints作爲輸入,並且隨着TeamPoints隊伍的增加/減少,它應該向上/向下移動隊列並顯示排名表。

+2

爲什麼一切都在單引號?即使是PHP標籤:o –

+0

php的報價是我的不好,我認爲我們必須使用引號來插入代碼。 –

回答

1

您正在使用mysqli_query進行多重查詢。所以你必須使用mysqli_multi_query

更改您的代碼:

<?php 
$con=mysqli_connect("","","",""); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$query="SET @rownum := 0; 
INSERT INTO team(TeamRank, TeamName) 
SELECT @rownum := @rownum + 1 AS TeamRank, TeamName 
FROM (SELECT SUM(TeamRank)AS TeamRank , TeamName 
FROM team 
GROUP BY TeamName 
ORDER BY TeamRank DESC) as result 
ON DUPLICATE KEY UPDATE TeamName = VALUES(TeamName); 
"; 

echo "<table border='1'> 
<tr> 
<th>Rank</th> 
<th>TeamID</th> 
<th>TeamName</th> 
<th>Total Points</th> 
</tr>"; 

if (mysqli_multi_query($con,$query)) { 

    do { 
     /* store first result set */ 
     if ($result = mysqli_store_result($con)) { 
      while ($row = mysqli_fetch_row($result)) { 
       echo "<tr>"; 
       echo "<td>" . $row['TeamRank'] . "</td>"; 
       echo "<td>" . $row['TeamID'] . "</td>"; 
       echo "<td>" . $row['TeamName'] . "</td>"; 
       echo "<td>" . $row['TeamPoints'] . "</td>"; 
       echo "</tr>"; 
      } 
      mysqli_free_result($result); 
     } 
    } while (mysqli_next_result($con)); 
} 
echo "</table>"; 


mysqli_close($con); 
?> 
+0

好的謝謝你的答覆Mehdi! 但我不知道代碼如何,它不增加TeamRank,它創建了兩個TeamRanks和TeamName的列。 我想要做的就是以TeamName,TeamWins,TeamLoss和TeamPoints作爲輸入,並且隨着TeamPoints增加/減少應該向上/向下移動排名的團隊。 –

+0

這是另一回事,所以你的主要問題解決了,所以請選擇作爲接受的答案,所以我會看看下一個問題 – Mehdi

+0

完成.......... –