-6ren">
gpt4 book ai didi

php - 无法弄清楚如何运行 mysqli_multi_query 并使用上次查询的结果

转载 作者:行者123 更新时间:2023-11-29 16:02:50 24 4
gpt4 key购买 nike

我以前从未使用过 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 个查询的最终结果并将它们放入我的表中。

上面的代码实际上根本不起作用,我只是收到以下错误:

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

有什么帮助吗?

最佳答案

来自manual :mysqli_multi_query() 返回一个 bool 表示成功。

To retrieve the resultset from the first query you can use mysqli_use_result() or mysqli_store_result(). All subsequent query results can be processed using mysqli_more_results() and mysqli_next_result().

这是一个返回多重查询的最后结果的函数:

function mysqli_last_result($link) {
while (mysqli_more_results($link)) {
mysqli_use_result($link);
mysqli_next_result($link);
}
return mysqli_store_result($link);
}

用法:

$link = mysqli_connect();

$query = "SELECT 1;";
$query .= "SELECT 2;";
$query .= "SELECT 3";

mysqli_multi_query($link, $query);
$result = mysqli_last_result($link);
$row = $result->fetch_row();
echo $row[0]; // prints "3"

$result->free();
mysqli_close($link);

关于php - 无法弄清楚如何运行 mysqli_multi_query 并使用上次查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56028545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com