Do Thanh Dat Do Thanh Dat - 8 months ago 30
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";
if(mysql_num_rows($query) != "")
echo $row['score'];

but something wrong:

Warning: mysql_num_rows() expects parameter 1 to be resource

Thank you so much !


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