Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8387335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:10:56+00:00 2026-06-09T18:10:56+00:00

Here is my PHP database structure: http://pastebin.com/x89AdzfS I’ve been trying for hours to get

  • 0

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>";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:10:58+00:00Added an answer on June 9, 2026 at 6:10 pm

    Here is a relatively simple join to accomplish what I think you are after.

    $sql = $db->prepare("SELECT 
      title, 
      description, 
      courses.name
    FROM assignments
    JOIN courses ON courses.id = assignments.course_id
    JOIN enrollments USING(course_id)
    WHERE enrollments.student_id = ?");
    
    $sql->execute(array($login_id));
    

    My thought process with queries goes something like this:

    1. Decide which is the “main” table. You said “displays all assignments”, so it’s “assignments”
    2. That table is the first table after FROM
    3. Knowing that I have a certain student, find the shortest path to “student_id” which makes sense for the domain logic
    4. I see that “enrollments” has a student_id, and that my assignment and enrollments has a course_id; so that’s my path to student_id
    5. JOIN enrollments on the factors involved in it’s relationship to assignments (which SQL makes easy when the columns are the same name with USING()

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to insert a record into my database. here is my php
Here an example my database structure.. uid email name ------------------------------- 1 john@gmail.com John 2
here's the table structure -- phpMyAdmin SQL Dump -- version 3.4.10.1 -- http://www.phpmyadmin.net --
Here is a confirmed bug report with Oracle: http://bugs.mysql.com/bug.php?id=67183 Situation When using an .Include
PHP/MySQLisolating database access in class - how to handle multiple row Selects Here’s a
Here is my php that retrieves image from mysql database. I want to resize
Here I have a simple php script which displays some values from a database
Here my code, I need to insert into mysql database I have mysql,php,android 1)
http://steph.net23.net/work.php here is my test link. This page has a jquery script in it
In host1 with domain http://domain1 I am using ajax with structure: index.php (that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.