Do Thanh Dat Do Thanh Dat - 7 months ago 24
SQL Question

What's wrong with my code ? Rank score mysql, php

here is my code

$sql="SET @rank=0; SELECT * FROM (SELECT *, @rank:=@rank+1 AS Rank FROM scoregame where userid=33 order by score DESC) AS t";
$query=mysql_query($sql);
if(mysql_num_rows($query) != "")
{
$stt=1;
while($row=mysql_fetch_array($query))
{
$stt++;
echo $row['score'];
}
}


but something wrong:

Warning: mysql_num_rows() expects parameter 1 to be resource


Thank you so much !

Answer

You try to run 2 queries at once. That does not work with this PHP function. But you can reduce it to one query

SELECT *, @rank:=@rank+1 AS Rank 
FROM scoregame 
cross join (select @rank := 0) r
where userid=33 
order by score DESC