我的查询在PhpMyAdmin中是正确的,在我的网站调用时出错了

时间:2014-06-07 00:56:57

标签: php mysql sql

这个完全相同的查询在从我的网站调用它时给我一个错误,但是如果我将它复制并粘贴到PhpMyAdmin sql编辑器中,那很好。它说选择等级,得分附近有语法错误......

     $dbResult = mysql_query
    ('
        set @rank=0;
        select rank, score from
            ( select @rank:=@rank+1 as rank, id, score
             from leaderboard 
             group by id
             order by score desc ) as rank_found 
        where id=10';
    ');

1 个答案:

答案 0 :(得分:4)

对于使用的语句集,您必须在php中使用'multi_query'

或者,您可以按如下方式修改查询:

select rank, score 
  from ( 
         select @rank:=@rank+1 as rank, id, score
           from leaderboard
              , ( select @rank := 0 ) as initializer 
          group by id
          order by score desc 
  ) as rank_found 
where id=10;

最好使用mysqli_*,因为mysql_*用法已被弃用。