Many-to-Many confusion...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Many-to-Many confusion...

Post by MiniMonty »

Hi all,

newb here learning as I go.

I've got a many-to-many db in which students, courses, course description and tutor
have relationships.

I'm trying to learn how to display various queries (and not doing very well).

My query looks like this:

Code: Select all

 
$id = mysql_real_escape_string($id);
$id = eregi_replace("`", "", $id);
$query = sprintf("SELECT c.* FROM students_to_courses stc INNER JOIN courses c ON stc.course_fk = c.course_id WHERE stc.student_fk = '$id'");
$result = mysql_query($query);
 
while ($row = mysql_fetch_assoc($result)) {
    $tutor = $row['tutor_fk'];
    $course_name = $row['course_name'];
    $course_descriptoin = $row['course_description'];
}
 
I want to display a list of courses that the member has signed up to.
So in the body of the html I tried this:

Code: Select all

 
<td colspan="4"><span class="orangeText"><?php print_r ("$course_name"); ?></span></td>
 
but it only lists one course that the member has signed up (and I know the member is signed up to three).

Any ideas ?

Best wishes
Monty
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Many-to-Many confusion...

Post by AbraCadaver »

Each time through the while loop it is fetching one row from the $result, so you need the HTML inside the while loop, or put the while loop in the HTML:

Code: Select all

while ($row = mysql_fetch_assoc($result)) {
    $tutor = $row['tutor_fk'];
    $course_name = $row['course_name'];
    $course_descriptoin = $row['course_description'];
    echo '<td colspan="4"><span class="orangeText">' . $course_name . '</span></td>';
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
MiniMonty
Forum Contributor
Posts: 196
Joined: Thu Sep 03, 2009 9:09 am
Location: UK

Re: Many-to-Many confusion...

Post by MiniMonty »

Thanks mate - that works sweetly.
Post Reply