title: User-defined MySQL variables in Laravel 3?
body: <p>I want to update the "rank" for a group of MySQL records with sequential numbers using a user-defined variable. The following query runs fine via the MySQL command line:</p>
  
  <pre><code>SET @rank:=0; UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=4 ORDER BY score DESC
  </code></pre>
  
  <p>But if I try and run it as a Fluent query using Laravel, it fails.</p>
  
  <pre><code>DB::query("SET @rank:=0; UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=4 ORDER BY score DESC");
  </code></pre>
  
  <p>Error message:</p>
  
  <pre><code>SQLSTATE[42000]: Syntax error or access violation: 1064 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 'UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=4 ORDER BY' at line 1
  
  SQL: SET @rank:=0; UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=4 ORDER BY score DESC
  
  Bindings: array (
  )
  </code></pre>
  
  <p><strong>[SOLVED]</strong></p>
  
  <p>DB::raw() to the rescue! The following works:</p>
  
  <pre><code>DB::query(DB::raw("SET @rank:=0"));
  DB::query("UPDATE scores SET rank=@rank:=@rank+1 WHERE game_id=4 ORDER BY score DESC");
  </code></pre>