我正在使用此PHP代码将数据从MySQL表导出到CSV文件
$file="jobs";
$sql="SELECT * FROM jobs";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$csv_output.=implode(";",array_keys($result))."\n";
do
{
// notice how there is no flag needed!
// also, you don't need to call 'array_values' when imploding
$csv_output.=implode(";",$result)."\n";
}
while( $result = mysql_fetch_assoc($rs));
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit();
但是我得到了这个输出:
sequence;customer;description;status;jobreceived;timebookedfor;bookedfor;site_contact;site_address;invoice_contact;invoice_address;quotedprice;cleardowndetails;industry;supplier_seq;notes;mo_number;invoice_postcode;file1;file2;file3;file4;file5;datejobadded;deleted;worksorder_seq;site_postcode 1;1;remedial works from original job.;3;2013-03-08;ASAP;2013-03-08;;48 Poole House, Godman Road, Chadwell Road RM16 4TQ ... ;;;;Attend site 13/03: Investigate and identify what immersion heater as required. *diagnose that only elements required Re-attending 14/03 PM to attend, supply and fit new element. 898001 £9.83 895003 £39.57 820009 £7.41 895002 £29.80;1;4;
RGK attending 14/03 pm to fit
Attend site 13/03:
Investigate and identify what immersion heater as required.
*diagnose that only elements required
Re-attending 14/03 PM
to attend, supply and fit new element.
;MO82058G12;;;;;;;0000-00-00;;;RM16 4TQ 2;1;Supply and fit toilet cistern;6;2013-03-20;TBC;2013-03-22;Angela Bone - 07980 006325;47 Keir Hardie House, Milford Road ;;;;Attend site supplied and fitted new toilet cistern.;1;4;
Job completed
;No MO code;;;;;;;0000-00-00;;;RM16 2QP 3;1;Recommendation from previous visit ;5;2013-03-08;ASAP;2013-03-08;01708853220 ;49 Dunkellin Way, South Ockendon, Essex ;;;;tenant now has water *recommend 2x new gate valves required and replace section of pipe work;1;;
space heating attending. 08/03/13 asap
**********************************recomendation ************************
If the problem persists - recommend 2x new gate valves required and replace section of pipe work RB 13.33 12/03
;;;;;;;;0000-00-00;yes;;RM15 5ES 5;1;No Electric to property;6;2013-03-08;ASAP;2013-03-08;;82 Nicholas Close, South Ockendon, Essex RM15 6NJ;;
为什么不保存CSV文件?
答案 0 :(得分:0)
$file="jobs";
$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: text/csv");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
$fp = fopen('php://output', 'w');
$sql = "SELECT * FROM jobs";
$rs = mysql_query($sql,$conn)
or die(mysql_error());
while( $result = mysql_fetch_assoc($rs))
{
fputcsv($fp, $result, ';');
}
fclose($fp);
exit();