-3
我正在使用以下代碼將數據庫表導出爲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>');
?>
我是新手。請幫助我..
@ user20232359723568423357842364那麼,什麼? –
如果要返回所有行,請不要聚合(SUM&GROUP BY)。 –
@ X.L.Ant我不只是想返回所有的行,我想使用主題的名稱作爲列標題,每個標記在每個標記下打分。同時,我想查找每個學生的排名。但有人已經三次投下了......我無望...... –