Rank variable value in MySQL is always being set to null
= is the assignement operator only for SET statements,in your case is a comparison operator,for SELECT use rank := rank+1

Example

SET @var=1;
SELECT @var = 1 + 1; -&gt; This compares @var which is one to 2 so it returns 0.


Result 0

SET @var=1;
SELECT @var := 1 + 1; -&gt; This actually adds up.


Result 2