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 7938829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:55:12+00:00 2026-06-03T22:55:12+00:00

Say I have a table called students, containing a list of all the students.

  • 0

Say I have a table called students, containing a list of all the students. Each student can have many courses stored in another table, identified by a student id.

I want to do a query which will get the row from the students table with the info of the student, and also get all of his courses, in one query. The output I want is something like this:

Array
(
    [0] => Array
        (
            [id] => 5
            [firstName] => Bob
            [lastName] => Smith
            [email] => ....
            [courses] => Array
                (
                    [0] => Array
                        (
                            [id] => 30
                            [name] => Test Course
                            [price] => 400
                        )

                    [1] => Array
                        (
                            [id] => 30
                            [name] => Test Course 2
                            [price] => 300
                        )

                )

        )

)

To get the info of students, I would do this query:

SELECT * FROM students WHERE something ='something'

To get the courses for a student, I would do this:

SELECT * FROM courses WHERE studentId = '5'

Is it possible to do both queries at once and get an output like the array above, or do I have to loop through each student and get its courses, then attach it to the array manuallly?

  • 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-03T22:55:13+00:00Added an answer on June 3, 2026 at 10:55 pm
    $query = "select * from students inner join courses on studentID = '5' where something  = 'something'"; 
    $result = mysql_db_query ($dbname, $query, $link); 
    $id = $row[0]; # corresponding to row in students database
    $firstName= $row[1]; # corresponding to row in students database
    $lastName = $row[2]; # corresponding to row in students database
    $email = $row[3]; # corresponding to row in students database
    while($row = mysql_fetch_array($result)) {
      echo 'id: '.$row[id];
      echo '<br>';
      echo 'name: '.$row[name];
      echo '<br>';
      echo 'price: '.$row[price];
      echo '<br>';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table called Event which has a to-many relationship with
Let's say that I have a table called Appointments, another called Clients and a
Lets say I have a table called orders, where each row stores an id,
Lets say I have a table called Employees , and each employee has a
Say I have a table called HoursCharged ChrgNum(varchar(10)) CategoryID(uniqueidentifier) Month(datetime) Hours(int) CategoryID is a
Say I have a table called Users, which contains your typical information: Id, Name,
Lets say I have one table called REVIEWS This table has Reviews that customers
Let's say I have a table called messages with the columns: id | from_id
Let's say I have a table called Users which represents registered users of a
In PostgreSQL 8.3, let's say I have a table called widgets with the following:

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.