Here is my PHP database structure: http://pastebin.com/x89AdzfS
I’ve been trying for hours to get a JOIN SQL query using PDO to work but have failed, so I need some help here.
I need a PDO query that displays all the “assignments” (title and description) which are assigned to a class id that the student is also enrolled in.
Please tell me if you need me to be more specific. Thanks!
EDIT
Here’s a query I did which displays the classes a person is in
$sql = $db->prepare("SELECT enrollments.*, courses.*, students.* FROM enrollments
LEFT JOIN courses ON enrollments.course_id = courses.id
LEFT JOIN students ON students.id = enrollments.student_id
WHERE enrollments.student_id = ?
");
$sql->execute(array($login_id));
while($row = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<li><a href='class.php?id=".$row['course_id']."'><i class='icon-book'></i>".$row['name']."</a></li>";
}
Here is a relatively simple join to accomplish what I think you are after.
My thought process with queries goes something like this:
The difference from your query (as I see it), is to use your absolute destination entity as the original FROM table, and then join from there. If you want assignments, the query should start with
FROM assignments, then use joins to pull in the data you need to use to limit the results.