查询没有输出所有细节?

时间:2011-11-02 17:32:56

标签: php mysql database

以下是我的询问:

由于Mark Ba​​nnister尝试使用MYSQL工作台,因此找到了问题。它不再显示一行,它显示下面所需的行:

Course: INFO101 - Bsc Information Communication Technology Year: 3
Student: Mayur Patel (u0867587)

Module: CHI2550 - Modern Database Applications 
Session: AAB 72 (A)

Course: INFO101 - Bsc Information Communication Technology Year: 3
Student: Mayur Patel (u0867587)

Module: CHI2513 - Systems Strategy 
Session: AAD 61 (B)

Course: INFO101 - Bsc Information Communication Technology Year: 3
Student: Mayur Patel (u0867587)

Module: CHI2550 - Modern Database Applications 
Session: AAE 67 (B)

所以我现在真正需要帮助的是如何使用php显示它,这样它只显示课程详细信息和学生详细信息一次,它只显示课程中的每个模块,并显示每个模块低于每个模块它属于:

上面的输出应该如下所示:

 Course: INFO101 - Bsc Information Communication Technology Year: 3
    Student: Mayur Patel (u0867587)

    Module: CHI2550 - Modern Database Applications 
    Session: AAB 72 (A)
    Session: AAE 67 (B)

    Module: CHI2513 - Systems Strategy 
    Session: AAD 61 (B)

用于输出结果的PHP代码:

$output1 = "";

        while ($row = mysql_fetch_array($result)) { //$result is the query

    $output1 .= "
          <p><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>Year:</strong> {$row['Year']}<br/>
          <strong>Student:</strong> {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})
          </p>";

    $output1 .= "      
    <p><strong>Module:</strong> {$row['ModuleId']} - {$row['ModuleName']} <br/>
          <strong>Session:</strong> {$row['SessionId']} {$row['Mark']} ({$row['Grade']})
          </p>";
            }

            echo $output1;

谢谢

1 个答案:

答案 0 :(得分:0)

我想你已经用sql语句命令你的结果集升序

ORDER BY `CourseName` ASC

所以,问题在于结果集的第一行: Bsc信息通信技术年份:3 原因是您已经在结果集上执行了某些操作。 转到while之前的第一行:

//do this before anything:
mysql_data_seek($result, 0);
//now, you are at first row
$output1 = "";
while ($row = mysql_fetch_array($result)) { //$result is the query
    $output1 .= "
       <p><strong>Course:</strong> {$row['CourseId']} - {$row['CourseName']} <strong>
       Year:</strong> {$row['Year']}<br/><strong>Student:</strong> 
       {$row['StudentForename']} {$row['StudentSurname']} ({$row['StudentUsername']})
       </p>";
    $output1 .= "      
    <p><strong>Module:</strong> {$row['ModuleId']} - {$row['ModuleName']} <br/>
    <strong>Session:</strong> {$row['SessionId']} {$row['Mark']} ({$row['Grade']})
    </p>";
            }
echo $output1;

忽略我的换行符。