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.
Update: After much troubleshooting I’ve found the cause 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?
The query is as follows:
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'
The query executes correctly within Phpmyadmin and similar 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)
The php query is:
$result = mysqli_query($link, $queryAsAbove);
The query is 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.
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 here.
Error is as follows:
"You have an error...the right syntax to use near 'SELECT rank FROM (SELECT @rank :=
@rank +1 AS rank, location, filename' at line 2"
(Only has error when ran using php, not if query copied into PMA)
Thank you for your time, much appreciated.
Didn’t you meant to use multi_query() ?
Because mysqli_query can’t run more than one query