简体   繁体   English

MySQLi multi_query()好奇心

[英]MySQLi multi_query() curiosity

Can someone explain me what does mysqli multi_query() is being used for? 有人可以解释一下mysqli multi_query()是什么? 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: 我不理解简单的MySQLi查询是否支持multimle语句,还是我不得不使用multi_query()来进行如下查询:

   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. 我问这是因为运行$mysqli->query(ABOVE_STATEMENT)返回语法错误..即使在phpMyAdmin中运行相同的语句(使用mysqli扩展名设置),它也会成功返回所需的数据。

You do not have to use multi_query to do that. 您不必使用multi_query来做到这一点。 The @row variable keeps its value for your whole session (ie until you disconnect from MySQL). @row变量在整个会话中保持其值(即直到您与MySQL断开连接)。

You may submit the SET statement and the SELECT statement as separate queries. 您可以将SET语句和SELECT语句作为单独的查询提交。

$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. 然后告诉他们您不打算在一个PHP请求中运行一千个查询,因为这将对性能差异产生重大影响。

multi_query is if you are doing something such as: multi_query是如果您正在执行以下操作:

select * from somewhere;
select id from somewhere;
select names from somewhere;

Check out http://www.w3schools.com/php/func_mysqli_multi_query.asp 查看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. 请注意,必须如何存储第一个结果,然后调用mysqli_next_result()来获取下一个结果。 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. 编辑:马克B是正确的注释中,您在技术上通过设置该行使用2条语句。 In that case you are using multiple queries. 在这种情况下,您将使用多个查询。 I didn't see that at first. 一开始我没看到。

mysqli_query indeed cannot execute your given queries. mysqli_query确实不能执行您给定的查询。

If you want to use mysqli_multi_query , then you can do the following 如果要使用mysqli_multi_query ,则可以执行以下操作

$mysqli->multi_query(ABOVE_STATEMENT);
$mysqli->next_result(); # skip the first query result
$rs = $mysqli->store_result();
while ($row = $rs->fetch_row()) {
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM