title: Nested/Subqueries cuasing syntax erros using mysqli extension; though syntactically correct
body: <p>I'm using the mysqli improved extension (procedural style) and have just ran into an anomaly when running sub-queries through the mysqli_query() function. </p>
  
  <p><strong>Update: After much troubleshooting I've found the <em>cause</em> of the error is setting the '@rank' variable. I tried a query without setting such '@var's and works fine. But they are needed...  - Does PHP not pass 'SET @example = x' correctly to mysql, or need to be escaped somehow?</strong></p>
  
  <p>The query is as follows: </p>
  
  <pre><code>SET @rank = -1; 
  SELECT rank FROM (
  SELECT @rank := @rank +1 AS rank, location, fileName 
  FROM assets WHERE idUser = '32'
  ORDER BY UploadTimestamp DESC) AS rank_number 
  WHERE photoFileName = 'filename.ext'
  </code></pre>
  
  <p>The query executes correctly within Phpmyadmin and <a href="https://stackoverflow.com/questions/2348925/simple-insert-works-with-phpmyadmin-but-not-with-php">similar</a> posts direct towards the multi_query method however this doesn't seem to be a query that can be split into multiple queries? It bound to have a simpler solution (perhaps)</p>
  
  <p>The php query is: </p>
  
  <pre><code>$result = mysqli_query($link, $queryAsAbove);
  </code></pre>
  
  <p>The query <em>is</em> dynamic (it gets passed a filename) though this is escaped and not the cause.
  Finally the query is within a mysqli_fetch_array loop though variables have been checked for same-name conflict.</p>
  
  <p>The sql @rank variable is set to -1 in order for the fetched array to be indexed at 0. The query is simply uses to identify the row number from a SELECT query as described <a href="http://www.fromdual.com/ranking-mysql-results" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Error is as follows:</p>
  
  <pre><code>"You have an error...the right syntax to use near 'SELECT rank FROM (SELECT @rank :=
  @rank +1 AS rank, location, filename' at line 2"
  </code></pre>
  
  <p>(Only has error when ran using php, not if query copied into PMA)</p>
  
  <p>Thank you for your time, much appreciated.  </p>