我正在使用以下代码将数据库表导出到 excel。这段代码的问题是它在 Excel 中只返回一行。我不知道代码有什么问题。mysql代码或php代码。我一直在尝试更正代码。但我还做不到。所以我真的需要你的帮助。这是代码。
<?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.' <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> </td>';
else
$output .= "<td>$row[$j]</td>";
}
print(trim($output))."</tr>\t\n";
}
echo('</table>');
?>
我是新手。请帮我..