如何突破SQL查询无限循环

时间:2018-08-28 18:50:26

标签: php sql

运行此命令将输出100到165次的每个结果。似乎是一个无限循环,但我不知道如何突破它或确定问题所在。任何帮助表示赞赏。

    $sql = "SELECT *
FROM timetable t, class c, room r
WHERE c.id = t.class AND t.week = '".$_GET['week']."' AND t.id = '".$_GET['class']."' 
ORDER BY CASE
          WHEN Day = 'Sunday' THEN 1
          WHEN Day = 'Monday' THEN 2
          WHEN Day = 'Tuesday' THEN 3
          WHEN Day = 'Wednesday' THEN 4
          WHEN Day = 'Thursday' THEN 5
          WHEN Day = 'Friday' THEN 6
          WHEN Day = 'Saturday' THEN 7
      ELSE Day
     END ASC, Hour;
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "Timetable of the class: ".$_GET['class']."";
    echo "<table>";
    while($row = $result->fetch_assoc()) {


        echo "<tr>";
        echo "<td>" . $dan . "</td><td>". $row["ura"] . "</td><td>". $row["razred"] . "</td><td>" . $ucilnica . "</td><td>" . $row["ucitelj"] . "</td>";
        echo "</tr>";
        }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
}
?>

1 个答案:

答案 0 :(得分:1)

您缺少指向room表的链接,因此它正在为每种组合选择所有房间。

SELECT *
FROM timetable t, class c, room r
WHERE r.? = ?.? AND c.id = t.class AND t.week ...

您也可以尝试新的JOIN表示法...

SELECT *
FROM timetable t
JOIN class c ON c.id = t.class
JOIN room r ON ?
WHERE t.week...

我还建议研究准备好的语句以防止SQL注入等。