Question

Guys I'm new here and new to MySQL too ..

So I am trying to create a database which manages a team record.The database contains a table named team with a set of columns as follow,

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

So the agenda is to rank the teams on the basis of points, the greater the points higher the rank.

<?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);

Im recieveing this Error

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result

Where am I Wrong? If there a problem with the code please guide me.

P.S[EDIT]: All I want to do is take TeamName,TeamWins,TeamLoss and TeamPoints as input, and as the TeamPoints increase/decrease of a team its supposed to move the rank upwards/downwards and display a Ranking table.

Was it helpful?

Solution

You are using mysqli_query for multi query. so you have to use mysqli_multi_query.

change your code to:

<?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);
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top