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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:37:13+00:00 2026-06-16T16:37:13+00:00

I have these two tables: sections – {id,title,description} topics – {id,section_id,topictitle} Each section has

  • 0

I have these two tables:

sections - {id,title,description}
topics - {id,section_id,topictitle}

Each section has multiple topics in the topics table.

after reading and trying I found using left join is the right way

$sql = 'select * from sections left join topics on sections.id=topics.sections_id';
$query = mysql_query($sql);
$section = array();
$i=0;
while ($sections = mysql_fetch_assoc($query)) {
    $sl = array(
        'title' => $sections['title'],
        'topics'=> $sections['topictitle'],
    );
    $section[$i++] = $sl;
}

Is this the right code to view all sections and topics??

  • 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-16T16:37:14+00:00Added an answer on June 16, 2026 at 4:37 pm

    You’re selecting from sections and joining topics so you will only ever get one topic linked to each section, it won’t work if you have multiple topics linked to one section (which I think you have).

    Do it the other way around – select from topics and join sections:

    select * from topics left join sections on sections.id=topics.section_id
    

    Keep in mind that if you have a section that has no topics linked, you won’t get that section in your result set. If you want that then it’s worth selecting the sections first.

    Also, instead of incrementing like that, you can just push to the array – you don’t need to manually manage the index:

    // $section[$i++] = $sl;  // <-- your way
    
    $section[] = $sl;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple UITableView with two sections. Each section has a header which
I have these two tables people ============ id, name and answer_sheets ============ id, person_id,
If I have these two tables: <table> <tr> <td>Small Length Content</td> </tr> </table> <table>
Hi, I have these two tables: users and friends (friend_status = 1 means the
Hi, I have these two tables: users and friends (friend_status = 1 means the
This is SQL Server 2008. I have these two tables and a join: DECLARE
I have two tables with a single common field. Here is how these two
We have two tables Family and Member, the relation between these two is Family
I have tables like these two test_table date student test 2012-05-31 Alice Math 2012-05-31
I have two tables (using table variables for illustration. You can run these directly

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.