Php 我有两个表(expenses,expdescription)。我的问题是如何在按expenses description分组的表中显示费用 while($row=mysqli\u fetch\u数组($query)) { //$sql3='select*来自描述,其中descid='。$row['descid'].'; $query3=mysqli\u查询($conn,$sql3); $row2=mysqli_fetch_数组($query3); $total=$total+$row['amount']; 回声' “.$row[“金额”]” “.$row2['description']” “.$row['info']” “.$row['DATE']” “.$total.”; ?>

Php 我有两个表(expenses,expdescription)。我的问题是如何在按expenses description分组的表中显示费用 while($row=mysqli\u fetch\u数组($query)) { //$sql3='select*来自描述,其中descid='。$row['descid'].'; $query3=mysqli\u查询($conn,$sql3); $row2=mysqli_fetch_数组($query3); $total=$total+$row['amount']; 回声' “.$row[“金额”]” “.$row2['description']” “.$row['info']” “.$row['DATE']” “.$total.”; ?>,php,sql,Php,Sql,您必须生成另一个按描述分组的数组,或者只查询另一个SQL语句,以读取分组的行。由于您已经查询了所有数据,我建议使用一个按描述分组的简单数组 while($row = mysqli_fetch_array($query)) { //$sql3 ='select * from description where descid = '.$row['descid'].' '; $query3 = mysqli_query($conn,$sql3);

您必须生成另一个按描述分组的数组,或者只查询另一个SQL语句,以读取分组的行。由于您已经查询了所有数据,我建议使用一个按描述分组的简单数组

    while($row = mysqli_fetch_array($query))
       {
        //$sql3 ='select * from description where descid = '.$row['descid'].' ';
        $query3 = mysqli_query($conn,$sql3);
        $row2 = mysqli_fetch_array($query3);
        $total =$total + $row['amount'];
        echo ' <tr>
        <td style="text-align:center">'.$row['amount'].'</td>
        <td style="text-align:center">'.$row2['description'].'</td>
        <td style="text-align:center">'.$row['info'].'</td>
        <td style="text-align:center">'.$row['DATE'].'</td>
        <td style="text-align:center">'.$total.'</td>';

      
        ?>
        <td><a href="insert.php?delete=<?php echo $row['outid']; ?>"
        class ="btn btn-danger">delete</a></td>
        <?php
        echo '
        </tr>';
       
     }
    } 
另外,另一个解决方案可能是一个简单的SQL语句

$grouped = [];
$data = mysqli_fetch_array($query);

foreach ($data as $row) {
    if (!isset($grouped[$row['description'])) {
        $grouped[$row['description]] = 0;
    }

    $grouped[$row['description'] += $row['amount'];
}

// process your templating here
foreach ($data as $row) {
    ...
}

您的问题不是php代码而是SQL查询。您是否尝试在PHPMyAdmin中制定SQL查询,或者这是您的问题
SELECT SUM(amount) AS sum FROM table WHERE description_id = :description_id