2

我有一个似乎没有获取第二行 ID 的 SQL 查询。我该如何解决?

               $sql = "SELECT 
                        *
                        FROM ts_request
                        INNER JOIN ts_day
                        ON ts_request.day_id = ts_day.id
                        INNER JOIN ts_period
                        ON ts_request.period_id = ts_period.id
                        INNER JOIN ts_allocation
                        ON ts_request.id = ts_allocation.request_id
                        WHERE ts_request.round=:round
                        AND ts_request.dept_id=:dept
                        ORDER BY ts_request.module_id ASC";
        }
        $stm = $pdo->prepare( $sql );
        $stm->execute( array( ':round' => 'P', ':dept' => $loggedin_id  ) );
        $rows = $stm->fetchAll();        


        foreach ($rows as $row) 
        {
        $row_id = $row['id'];
        echo '<tr align="center">'; 
        echo '<td>'.$row['module_id'].'</td>';
        echo '<td>'.$row['day'].'</td>';    
        echo '<td>'.$row['period'].'</td>';
        echo '<td>';

            $sql = "SELECT 
                    *
                    FROM ts_roompref
                    WHERE request_id=:request_id";
            $stm = $pdo->prepare( $sql );
            $stm->execute( array( ':request_id' => $row_id ) );
            $rows = $stm->fetchAll();           
    foreach ($rows as $row) 
            {
                if ($row['room_id']!="0")
                    echo $row['room_id'].'<br>';
            }

        echo '</td>';

        echo '<td>';

        $sql = "SELECT 
                        *
                        FROM ts_allocation
                        WHERE request_id=:request_id";
        $stm = $pdo->prepare( $sql );
        $stm->execute( array( ':request_id' => $row_id ) );
        $rows = $stm->fetchAll();           
        foreach ($rows as $row) 
        {
            echo $row['status'];
        }

        echo '</td></tr>';
        };
4

1 回答 1

1

您正在为数组使用foreach循环,$rows并在其中覆盖$rows变量。所以只需为内部循环使用另一个变量名:

$rows = $stm->fetchAll();
foreach ($rows as $row)
{
    // ...
    $rows2 = $stm->fetchAll();
    foreach ($rows2 as $row2)
    {
        // ...
    }
}
于 2013-02-18T14:36:03.330 回答