I have three tables in my database. Students, modules and relations. Each row of the relations table contains a student id and a module id (i.e it contains the modules that a student takes).
I am trying to loop through the relations table and find all the modules that a particular student takes, then print out the details of each module (e.g the name and module code).
I am trying to do this with a nested loop however the inside loop is only running once and printing out the first module. I think this is something to do with having a second sql query inside my while loop and $m_id isnt getting updated with each iteration. Here is my code so far
<?php
include 'connect.php';
$sql = "SELECT * FROM relations WHERE student_id = '1'";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
$m_id = $row["module_id"];
$sql = "SELECT * FROM modules WHERE module_id = $m_id";
$result = $conn->query($sql);
if($result->num_rows > 0) {
while($row = mysqli_fetch_array($result)) {
echo $row["module_code"];
}
}
}
}
?>
Can anyone help me out with this?
INNER JOIN