gpt4 book ai didi

php - 在mysql查询中循环数组并获取结果

转载 作者:行者123 更新时间:2023-11-29 23:46:24 25 4
gpt4 key购买 nike

我正在尝试循环 mysql 查询并在新行上显示每个结果。我知道我丢失了一些图片,我需要在 mysql 中设置变量吗?

$value = array(B1,B2,B3,B4);
$query = "SELECT SUM($value[0]) AS ".$value[0]."_SUM, "
."SUM(answer_value) AS ".$value[0]."_ANSWER_SUM "
."FROM questions q JOIN answers a ON q.question_id = a.question_id "
."WHERE $value[0]=1";

$result = mysqli_query($con, $query);

while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$value[0]."</td>";
echo "<td>".$row['B1_ANSWER_SUM']."</td>";
echo "<td>".$row['B1_SUM'] ."</td>";
echo '</tr>' ;
}

我想得到这个结果:

B1 B1_ANSWER_SUM B1_SUM
B2 B2_ANSWER_SUM B2_SUM
B3 B2_ANSWER_SUM B2_SUM

最佳答案

您需要循环遍历数组并查询数组中的每个项目。见下文:

$value = array('B1', 'B2', 'B3', 'B4');

foreach ($value as $v) {
$query = "SELECT SUM($v) AS ".$v."_SUM, SUM(answer_value) AS ".$v."_ANSWER_SUM FROM questions q JOIN answers a ON q.question_id = a.question_id WHERE $v=1";

$result = mysqli_query($con, $query);

while($row = mysqli_fetch_array($result)) {
echo "<tr><td>".$v."</td>";
echo "<td>".$row[$v.'_ANSWER_SUM']."</td>";
echo "<td>".$row[$v.'_SUM'] ."</td>";
echo '</tr>' ;
}
}

关于php - 在mysql查询中循环数组并获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25913439/

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