简体   繁体   English

嵌套在MySQL数据检索中的while循环

[英]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") 我正在使用嵌套的while循环,以便从2个不同的表(学生,年级,其中两个表都通过主键-“ 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代码

<?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: 不要使用嵌套循环,请使用JOIN在一个查询中获取所有结果:

$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. 我认为您正在第二个循环中覆盖$ rows变量。 try using 2 different variables 尝试使用2个不同的变量

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. 将成绩查询放入学生循环中,并为其分配学生ID,也不要在循环中重新分配$rows变量。

$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. 这里您覆盖了$rows变量,因此请尝试使用其他变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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