无法弄清楚如何运行 mysqli_multi_query 并使用上次查询的结果
我以前从未使用过 mysqli_multi_query ,它让我难以置信,我在网上找到的任何例子都不能帮助我准确地弄清楚我想要做什么。
这是我的代码:
<?php
$link = mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$agentsquery = "CREATE TEMPORARY TABLE LeaderBoard (
`agent_name` varchar(20) NOT NULL,
`job_number` int(5) NOT NULL,
`job_value` decimal(3,1) NOT NULL,
`points_value` decimal(8,2) NOT NULL
);";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`, `job_number`, `job_value`, `points_value`) SELECT agent_name, job_number, job_value, points_value FROM jobs WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`) SELECT DISTINCT agent_name FROM apps WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$agentsquery .= "SELECT agent_name, SUM(job_value), SUM(points_value) FROM leaderboard GROUP BY agent_name ORDER BY SUM(points_value) DESC";
$i = 0;
$agentsresult = mysqli_multi_query($link, $agentsquery);
while ($row = mysqli_fetch_array($agentsresult)){
$number_of_apps = getAgentAppsWeek($row['agent_name'],$weeknum,$current_year);
$i++;
?>
<tr class="tr<?php echo ($i & 1) ?>">
<td style="font-weight: bold;"><?php echo $row['agent_name'] ?></td>
<td><?php echo $row['SUM(job_value)'] ?></td>
<td><?php echo $row['SUM(points_value)'] ?></td>
<td><?php echo $number_of_apps; ?></td>
</tr>
<?php
}
?>
我想做的就是运行多个查询,然后使用这 4 个查询的最终结果并将它们放入我的表中。
上面的代码实际上根本不起作用,我只是收到以下错误:
警告:mysqli_fetch_array() 需要 参数1为mysqli_result, 布尔值给出 C:\xampp\htdocs\Hydroboard\Hydro_reporting_2010.php 在第 391 行
什么帮助吗?
I've never used mysqli_multi_query before and it's boggling my brain, any examples I find on the net aren't helping me to figure out exactly what it is I want to do.
Here is my code:
<?php
$link = mysqli_connect("server", "user", "pass", "db");
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$agentsquery = "CREATE TEMPORARY TABLE LeaderBoard (
`agent_name` varchar(20) NOT NULL,
`job_number` int(5) NOT NULL,
`job_value` decimal(3,1) NOT NULL,
`points_value` decimal(8,2) NOT NULL
);";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`, `job_number`, `job_value`, `points_value`) SELECT agent_name, job_number, job_value, points_value FROM jobs WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$agentsquery .= "INSERT INTO LeaderBoard (`agent_name`) SELECT DISTINCT agent_name FROM apps WHERE YEAR(booked_date) = $current_year && WEEKOFYEAR(booked_date) = $weeknum;";
$agentsquery .= "SELECT agent_name, SUM(job_value), SUM(points_value) FROM leaderboard GROUP BY agent_name ORDER BY SUM(points_value) DESC";
$i = 0;
$agentsresult = mysqli_multi_query($link, $agentsquery);
while ($row = mysqli_fetch_array($agentsresult)){
$number_of_apps = getAgentAppsWeek($row['agent_name'],$weeknum,$current_year);
$i++;
?>
<tr class="tr<?php echo ($i & 1) ?>">
<td style="font-weight: bold;"><?php echo $row['agent_name'] ?></td>
<td><?php echo $row['SUM(job_value)'] ?></td>
<td><?php echo $row['SUM(points_value)'] ?></td>
<td><?php echo $number_of_apps; ?></td>
</tr>
<?php
}
?>
All I'm trying to do is run a multiple query and then use the final results from those 4 queries and put them into my tables.
the code above really doesn't work at all, I just get the following error:
Warning: mysqli_fetch_array() expects
parameter 1 to be mysqli_result,
boolean given in
C:\xampp\htdocs\hydroboard\hydro_reporting_2010.php
on line 391
any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
从 手册:
mysqli_multi_query()
返回一个bool
指示成功。这是一个返回多重查询的最后结果的函数:
用法:
From the manual:
mysqli_multi_query()
returns abool
indicating success.Here is a function that returns the last result of a multi-query:
Usage:
好吧,经过一些摆弄、试验和错误,并参考我在谷歌搜索中遇到的另一篇文章,我已经成功解决了我的问题!
这是新代码:
在为每个查询多次粘贴 mysqli_next_result 后,它神奇地工作了!耶!我明白它为什么有效,因为我告诉它跳到下一个结果 3 次,所以它跳到查询 #4 的结果,这是我想要使用的结果。
不过,对我来说似乎有点笨重,应该只有一个类似 mysqli_last_result($link) 之类的命令,或者如果你问我的话......
感谢 rik 和 f00 的帮助,我最终到达了那里:)
Okay after some fiddling around, trial and error and taking reference from another post that I came across in a Google search I've managed to solve my problem!
Here's the new code:
after sticking mysqli_next_result in there multiple times for each query it magically worked! yay! I understand why it works, because i'm telling it to skip to the next result 3 times, so it skips to the result for query #4 which is the one i want to use.
Seems a bit clunky to me though, there should just be a command for something like mysqli_last_result($link) or something if you ask me...
Thanks for the help rik and f00, I got there eventually :)
我将通过创建一个存储过程来简化您尝试执行的操作,该存储过程将生成领导者/代理统计信息,然后从您的 php (单个调用)中调用它,如下所示:
完整脚本此处: http://pastie.org/1451802
或者,您可以将各个查询合并到单个 select/group by 语句中。
请参阅此处: http://pastie.org/1451842
存储过程
PHP 脚本
现在更简单了 - 希望它有所帮助:)
I would simplify what you're trying to do by creating a stored procedure which will produce the leader/agent stats and then just call it from your php (single call) as follows:
full script here : http://pastie.org/1451802
alternatively, you could probably combine your individual queries into a single select/group by statement.
see here : http://pastie.org/1451842
Stored procedure
PHP script
Kinda simpler now - hope it helps :)
将结果存储在变量中并最终使用该变量。
Store result in a variable and in the end use that variable.
我想整理海报目前接受的解决方案,使其与我认为的最佳实践更加一致。
我认为这更容易阅读。
I'd like to tidy up the poster's currently accepted solution to align it more with what I consider to be best practice.
IMO this is just easier to read.
我想我也会对这个问题表示赞同(嘿,这仍然是程序性的,而且都是爵士乐)。这并不是万无一失的,但我已经与 MySQLi 进行过斗争,它是一群与 multi_query 相关的快乐的人,但我无法让它按照我想要的方式很好地发挥作用,或者具有我需要的灵活性。我看到了几个例子,其中一些程序员只是简单地运行
explode(';', $sql_statements)
这让我的眼睛流血,因为这是多么可怕的错误。我的解决方案可能不适合你,但这对我有用。 (不,它也不是防弹的,但适合我的特定应用程序)。
I suppose I'll throw my hat in the ring on this question too (and hey it's still procedural and all that jazz). This is not fool proof, but I have fought and fought with MySQLi and it's band of merry men involved with multi_query and I couldn't get it to play nicely the way I wanted to, or have the flexibility that I needed. I saw several examples where some programmers were simply running
explode(';', $sql_statements)
which made my eyes bleed with how horribly wrong that can be.My solution may not work for you, but this worked for me. (no it's not bulletproof either, but does the job for my particular application).