Your
while
loop depends on the
$stmt
variable to contain the results of the query listing all of the classes, yet within the loop you're overwriting the results stored there with another block:
while($query = $stmt->fetch(PDO::FETCH_ASSOC)){
..
$stmt = $pdo->prepare("SELECT count(*) FROM booking WHERE class_id=?");
$stmt->execute([$classId]);
..
}
You need to consider naming your variables differently, perhaps in a way that identifies what those variables store. Be aware that anything that happens within the
while
loop could affect variables outside of that loop.