如何在php中使用sql变量传递sql查询

I want to pass following sql query in php

SET @rank=0; SELECT @rank:=@rank+1 AS rank, `percentage_obtained`  FROM `result`  ORDER BY percentage_obtained DESC

As here

mysql_query('SET @rank=0; SELECT @rank:=@rank+1 AS rank, `percentage_obtained`  FROM `result`  ORDER BY percentage_obtained DESC') or die(mysql_error());

But it throws following error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @rank:=@rank+1 AS rank, percentage_obtained FROM result ' at line 1

However it works when I run the query directly inside the database. Please assist.

mysql_query() doesn't support multiple queries, so you need to separate them. Also you can use mysqli_multi_query - but note, that you need mysqli instead of mysql

So

mysql_query('SET @rank=0') or die(mysql_error());
mysql_query('SELECT @rank:=@rank+1 AS rank, `percentage_obtained`  FROM `result`  ORDER BY percentage_obtained DESC') or die(mysql_error());

should handle this