I’m currently creating a page in PHP that will display a user and their ranks in my site’s high scores.
I had a problem where my MySQL query wasn’t functioning as intended, however, @VladBalmos solved my problem here: MySQL Highscores – User's Personal Ranks: Duplicate Entries Causing Incorrect Values
Now that I believe my MySQL query is functioning, I need to address the issues that arose with my PHP Code. Here is my code:
$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:=@rank + 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>";
}
…and the error I am receiving is:
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [path]/personal.php on line 156
Commands out of sync; you can’t run this command now
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [path]/personal.php on line 159
Commands out of sync; you can’t run this command now
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [path]/personal.php on line 161
Commands out of sync; you can’t run this command now
So, my question is, how do I correct this error?
you have to loop through the results of your multiple queries and then access the rows for each result set. Since the first query just sets a variable you have to skip to the next result set and fetch the actual rows (that’s why i did *while(mysqli_next_result…*)
read the docs at the php manual