为什么它只返回一行?

I am using the following code to export database table to excel. The problem with this code is that it returns only one row in Excel. I don't know what is wrong with the code. the mysql code or the php code. I have been trying to correct the code. But I couldn't do it yet. So I really need your help. Here is the code.

 <?php

$dbHost = 'localhost';          //  database host
$dbUser = 'root';       //  database user
$dbPass = 'passwrd';        //  database password
$dbName = 'mydatabase';         //  database name
$dbTable = 'table';         // table name

 $connection = @mysql_connect($dbHost, $dbUser, $dbPass) or die("Couldn't connect.");
 $db = mysql_select_db($dbName, $connection) or die("Couldn't select database.");

 $f=$_GET['fdate'];
 $t=$_GET['tdate'];
 $y=$_GET['Year'];

 $sql = "SELECT regd, Roll_no, Name_of_Student,
 SUM( IF( `Subject` = 'ENG-I', Mark_score, 0 ) ) AS Eng_I,
 SUM( IF( `Subject` = 'ENG-II', Mark_score, 0 ) ) AS Eng_II,
 SUM( IF( `Subject` = 'Mizo', Mark_score, 0 ) ) AS Mizo, 
 SUM( IF( `Subject` = 'Hindi', Mark_score, 0 ) ) AS Hindi,
 SUM( IF( `Subject` = 'EVS', Mark_score, 0 ) ) AS EVS,
 SUM( IF( `Subject` = 'Mathematics', Mark_score, 0 ) ) AS Maths,
 SUM( IF( `Subject` = 'GK', Mark_score, 0 ) ) AS GK,
 Sum(Full_mark) as Fullmark, Sum(Mark_score) as Totalscore, 
 Sum(Mark_score)/sum(Full_mark)*100 as Percentage FROM $dbTable
 WHERE Test_date Between '$f' and '$t' and Year='$y' and Class='IV' GROUP BY Subject && regd Order by Totalscore Desc";
 $result = @mysql_query($sql)   or die("Couldn't execute query:<br>".mysql_error().'<br>'.mysql_errno());

header('Content-Type: application/vnd.ms-excel');   
header('Content-Disposition: attachment; filename=Class-IV-'.date('d-M-Y').'.xls');
header('Pragma: no-cache');
header('Expires: 0');

echo '<table border="1"><caption><b>AR ELLS SCHOOL<br >CHALTLANG : MIZORAM<br />WEEKLY TEST BETWEEN '.$f.' AND '.$t.'&nbsp; <br /> CLASS : IV</b></caption><tr bgcolor="silver">';
for ($i = 0; $i < mysql_num_fields($result); $i++)   
    echo '<th>'.mysql_field_name($result, $i).'</th>';
print('</tr>');

while($row = mysql_fetch_row($result))
{
    //set_time_limit(60); 
    $output = '<tr style="background-color:white">';
    for($j=0; $j<mysql_num_fields($result); $j++)
    {
        if(!isset($row[$j]))
            $output .= '<td>&nbsp;</td>';
        else
            $output .= "<td>$row[$j]</td>";
    }
    print(trim($output))."</tr>\t
";
}
echo('</table>');
 ?>

I am a newbie. Please help me..

Your code has MULTIPLE problems with it.

1) Your are NOT generating an Excel file. You're generating some HTML that Excel happens to be able to pretend is really an Excel file.

2) Your generated HTML is invalid and broken. You cannot put <caption> where you are. An HTML table must look like

<table>
<tr>
   <td>...</td>
</tr>
</table>

<caption> must either be WITHIN the <td>, or completely OUTSIDE of the table.

3) You are vulnerable to SQL injection attacks. DO NOT USE THIS CODE in a production environment. You'll just get your server destroyed.

4) Have you done ANY debugging yourself? e.g. capture the generated query and run it yourself in an external tool? Did it return multiple rows there? If not, then it's your query that's broken, not the PHP code.