Reputation: 3576
Can someone explain me what does mysqli multi_query()
is being used for? I don't understand wether a simple MySQLi query supports multimle statements or am I forced to use multi_query()
in order to do a query like this:
SET @row:=0;
SELECT * FROM (
select id,
@row:=@row+1 AS `rank` FROM `users` WHERE `points` <= 63 ORDER BY `points` DESC
) AS number
WHERE id = 1
I'm asking this becuase running $mysqli->query(ABOVE_STATEMENT)
returns me a syntax error .. even though running the same statement in phpMyAdmin (set using mysqli extension) it succesfully returns the needed data.
Upvotes: 1
Views: 136
Reputation: 562240
You do not have to use multi_query to do that. The @row
variable keeps its value for your whole session (i.e. until you disconnect from MySQL).
You may submit the SET
statement and the SELECT
statement as separate queries.
$mysqli->query("SET @row:=0");
$mysqli->query("SELECT * ... ");
There are no good reasons to use multi-query. There are good reasons not to use multi-query.
If someone tells you that it's more efficient to use multi-query because "one round trip is faster than two," tell them to show you a benchmark that proves it. Then tell them that you're not planning on running a thousand queries in one PHP request, because that's what it would take for the difference in performance to have any significant impact.
Upvotes: 1
Reputation: 671
multi_query is if you are doing something such as:
select * from somewhere;
select id from somewhere;
select names from somewhere;
Check out http://www.w3schools.com/php/func_mysqli_multi_query.asp
Notice how you must store the first result, then call mysqli_next_result() to fetch the next results. It's throwing them all in 1 batch and returning them all back at you.
EDIT: Marc B is correct in the comment where you are technically using 2 statements by setting the row. In that case you are using multiple queries. I didn't see that at first.
Upvotes: 0
Reputation: 12772
mysqli_query
indeed cannot execute your given queries.
If you want to use mysqli_multi_query
, then you can do the following
$mysqli->multi_query(ABOVE_STATEMENT);
$mysqli->next_result(); # skip the first query result
$rs = $mysqli->store_result();
while ($row = $rs->fetch_row()) {
...
}
Upvotes: 0