我编写了一个用于导出 MySQL 表信息的类。这是成功执行此操作的最终函数:
class CSVExport{
private $file_name;
private $csv_data;
public function collectAllDataFromTable($table,$file_prefix,$date){
$sql = "SHOW COLUMNS FROM ".$table."";
$result = mysql_query($sql);
if(!$result){
die("Query failed ".mysql_error());
}
if(mysql_num_rows($result)>0){
$i = 0;
$csv_output = '';
while($row = mysql_fetch_assoc($result)){
$csv_output .= $row['Field'].",";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= $rowr[$j].", ";
}
$csv_output .= "\n";
}
// return $csv_output;
$this->file_name = $file_prefix."_".$date;
$this->csv_data = $csv_output;
} // End "exportAllDataFromTable" method
public function exportData(){
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$this->file_name.".csv");
print $this->csv_data;exit;
} // End 'exportData' method
} // End Db_Export
但是当我exportData()
第二次使用不同的参数调用时,它不会为我下载文件。
可能是什么问题呢 ?