简体   繁体   中英

Nested while loops in mysql data retrieval

I am using nested while loops in order to retrieve information from 2 different tables (students, grades where both of those tables are linked by primary key - "id")

I want it to output in the following format :

Student ID "123"

 - Grade 1 for ID "123"
 - Grade 2 for ID "123"

Student ID "555"

 - Grade 1 for ID "555"
 - Grade 2 for ID "555"

Student ID "666"

 - Grade 1 for ID "666"
 - Grade 2 for ID "666"

Currently it outputs like this:

Student ID "123"

 - Grade 1 for ID "123" 
 - Grade 2 for ID "123"
 - Grade 1 for ID "555"
 - Grade 2 for ID "555" 
 - Grade 1 for ID "666" 
 - Grade 2 for ID "666"

Student ID "555"
Student ID "666"

This is the PHP code

<?php
include ("../../php/account.php");

$dbh = mysql_connect ( $hostname, $username, $password )
                   or die ( "Unable to connect to MySQL database" );
mysql_select_db( $project );

$s = mysql_query("SELECT * FROM newstudent");
$r = mysql_query("SELECT * FROM grades");  

while($rows=mysql_fetch_array($s))
    {
    echo "Student ID: ".$rows['id']."<br/>";
    echo "First Name: ".$rows['firstname']."<br/>";
    echo "Last Name: ".$rows['lastname']."<br/>";
    echo "Email: ".$rows['email']."<br/>";
    echo "<br/>";

            while($rows=mysql_fetch_array($r))
            {
                echo "Subject: ".$rows['subject']."<br/>";
                echo "Grade One: ".$rows['gradeone']."<br/>";
                echo "Grade Two: ".$rows['gradetwo']."<br/>";
                echo "Grade Three: ".$rows['gradethree']."<br/>";
                echo "<br/>";                
            }

    }

?>

If anyone knows a solution please help me out! many thanks!

Don't use nested loops, use a JOIN to get all the results in one query:

$q = mysql_query("SELECT * FROM newstudent s
                  JOIN grades g ON s.id = g.student_id
                  ORDER BY s.id") or die (mysql_error());

$last_student = null;
while ($row = mysql_fetch_assoc($q)) {
    if ($row['id'] !== $last_student) {
        $last_student = $row['id'];
        echo "Student ID: ".$row['id']."<br/>";
        echo "First Name: ".$row['firstname']."<br/>";
        echo "Last Name: ".$row['lastname']."<br/>";
        echo "Email: ".$row['email']."<br/>";
        echo "<br/>";
    }
    echo "Subject: ".$row['subject']."<br/>";
    echo "Grade One: ".$row['gradeone']."<br/>";
    echo "Grade Two: ".$row['gradetwo']."<br/>";
    echo "Grade Three: ".$row['gradethree']."<br/>";
    echo "<br/>";                
}

您正在覆盖$rows变量。

you are overwriting the $rows variable in the second loop, i think. try using 2 different variables

edit: The inner loop simply states "print all the grades" , so it has no relation with the student you are examining in the outer loop.

There is surely the needing of a foreign key that allows you to take the grades of only the student you are taking from the first loop, as someone else noticed.

Put your grades query inside your students loop and assign the student ID to it, also don't reassign the $rows variable inside your loops.

$s = mysql_query("SELECT * FROM newstudent");


while($rows=mysql_fetch_array($s))
    {
    echo "Student ID: ".$rows['id']."<br/>";
    echo "First Name: ".$rows['firstname']."<br/>";
    echo "Last Name: ".$rows['lastname']."<br/>";
    echo "Email: ".$rows['email']."<br/>";
    echo "<br/>";

    $r = mysql_query("SELECT * FROM grades WHERE studentID = '" . $row['id'] . "'");  

            while($r=mysql_fetch_array($r))
            {
                echo "Subject: ".$r['subject']."<br/>";
                echo "Grade One: ".$r['gradeone']."<br/>";
                echo "Grade Two: ".$r['gradetwo']."<br/>";
                echo "Grade Three: ".$r['gradethree']."<br/>";
                echo "<br/>";                
            }

    }

Try this

<?php
include ("../../php/account.php");

$dbh = mysql_connect ( $hostname, $username, $password )
                   or die ( "Unable to connect to MySQL database" );
mysql_select_db( $project );

$s = mysql_query("SELECT * FROM newstudent");

while($rows=mysql_fetch_array($s))
    {
    echo "Student ID: ".$rows['id']."<br/>";
    echo "First Name: ".$rows['firstname']."<br/>";
    echo "Last Name: ".$rows['lastname']."<br/>";
    echo "Email: ".$rows['email']."<br/>";
    echo "<br/>";

$r = mysql_query("SELECT * FROM grades");  

            while($rows1=mysql_fetch_array($r))
            {
                echo "Subject: ".$rows1['subject']."<br/>";
                echo "Grade One: ".$rows1['gradeone']."<br/>";
                echo "Grade Two: ".$rows1['gradetwo']."<br/>";
                echo "Grade Three: ".$rows1['gradethree']."<br/>";
                echo "<br/>";                
            }

    }

?>

Here You are overriding the $rows variable.So try to use different variable names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM