我使用嵌套的while循环来检索来自2个不同表格的信息(学生,这两个表都通过主键链接的成绩 - “id”)
我希望它以下列格式输出:
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"
目前输出如下:
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"
这是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/>";
}
}
?>
如果有人知道解决方案,请帮助我!非常感谢!
答案 0 :(得分:9)
不要使用嵌套循环,使用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/>";
}
答案 1 :(得分:2)
您正在覆盖$rows
变量。
答案 2 :(得分:1)
我认为你在第二个循环中覆盖了$ rows变量。 尝试使用2个不同的变量
编辑: 内循环只是简单地说“打印所有成绩”,所以它与你在外循环中检查的学生没有任何关系。
肯定需要一个外键,只允许你从第一个循环中取得你正在学习的学生的成绩,就像其他人注意到的那样。
答案 3 :(得分:1)
将您的成绩查询放入学生循环中并为其分配学生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/>";
}
}
答案 4 :(得分:0)
试试这个
<?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/>";
}
}
?>
这里你覆盖$rows
变量。所以尝试使用不同的变量名。