How to output a row from Table B based on a value in a row for Table A for every value of table A (SQL in PHP)
11:01 15 Feb 2026

So I have a table with 2 columns, parent ID and course ID. there is also a table with several columns, the course table, which has course ID as a column. I can isolate the current parent ID based on the log in data. for every row in the first table matching the current parent ID, I want to output the relevant course data via the course ID associated. a parent ID can have many course IDs linked to it. I am programming in php linked to a html site, and my tables are in SQL. Im sorry if this isn't very clear, I am an A level CS student and this is my first time with any of these languages.



    
    
    University
    


    
query($sql); $row = $result->fetch_assoc(); $ParentID = $row['Variable']; $sql = "SELECT CourseID FROM parent_university_courses WHERE ParentID = '$ParentID'"; $result = $connection->query($sql); //$row = $result->fetch_assoc(); //$CourseID = $row['CourseID']; //Fetch course data from the database //$sql = "SELECT SubjectName, Description, Images, GradeRequirement, UCASRequirement FROM university_courses WHERE CourseID='$CourseID'"; //$result = $connection->query($sql); if($result->num_rows > 0) { //Output data of each row while($row = $result->fetch_assoc()) { $CourseID = $row['CourseID']; echo '
'; $sql = "SELECT SubjectName, Description, Images, GradeRequirement, UCASRequirement FROM university_courses WHERE CourseID='$CourseID'"; $result = $connection->query($sql); echo ''; echo '

' . $row["SubjectName"] . '

'; echo '

' . $row["Description"] . '

'; echo '

' . $row["CourseID"] . '

'; echo '

Grade requirement: ' . $row["GradeRequirement"] . '

'; echo '

UCAS requirement: ' . $row["UCASRequirement"] . '

'; echo '
'; echo '
'; } } else { echo "0 results"; } $connection->close(); ?>
php html sql heidisql