我正在使用嵌套的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/>";
}
}
?>
如果有人知道解决方案,请帮助我!非常感谢!