I need some assistance writing the sql select statement for a many to many database relationship.
I have the table1, table2, and table3 in mysql and want to output the bottom table in html. However, the relational structure is confusing me. Can you help me output the correct interests in the correct table cell? Currently, my sql select and fetch statement produce a table with multiple rows for each user, one with each interest.
I have tried every kind of join and array that I know to get this to output correctly. Can you explain what am I doing wrong? My current code is shown below:
$query = "SELECT *, table2.interest FROM table1
JOIN table3 ON table3.table1id = table1.id
JOIN table2 ON table3.table2id = table2.id
WHERE year=2017";
//Execute query
$qry_result = mysql_query($query) or die(mysql_error());
//Build Result String
$display_string = "<table border='1px'><tr><th>id</th><th>name</th> <th>string</th><th>year</th><th>interest</th></tr>";
$display_string .= "<tr><td>$row[id]</td><td>$row[name]</td> <td>$row[string]</td><td>$row[year]</td><td>$row[interests] </td></tr> </table>";
echo $display_string;