I constantly am using stackoverflow to help me but right now I am stumped - hopefully I can get some guidance on this issue:
I have two tables, one containing NFL Divisions the other containing NFL Teams. They are connected via the division_id field (primary key on Divisions table, foreign key on the Teams table).
What I would like to do is join the tables in a query and output the Teams by Division. I can accomplish this in PHP with two separate queries/loops but I want to know how/if I can accomplish this by joining the two tables in a single query instead.
Here is what I've got:
$query = "SELECT Divisions.division_id, Divisions.conference, Divisions.name,
Teams.name FROM Divisions INNER JOIN Teams ON
Teams.division_id = Divisions.division_id ORDER BY Divisions.division_id";
if( $result = $db->query($query) ){
while( $row = $result->fetch_object() ){
// here is where I'm stuck
}
}
I'm trying to get the final output to look similar to:
AFC North
Cleveland Browns
Pittsburgh Steelers
Baltimore Ravens
Cincinnati Bengals
AFC East
Buffalo Bills
Miami Dolphins
etc..
Major thank you in advance to anyone who can help me out with this! I haven't done any programming in over 2 years and just getting back into it.