简体   繁体   English

使用PHP将MySQL数据导出到CSV文件

[英]Export MySQL data to CSV file using PHP

I have a small problem with exporting MySQL data to CSV file, it print out the data correctly from the table but at the end of the csv file it prints also the html page code! 我在将MySQL数据导出到CSV文件时遇到一个小问题,它可以正确地从表中打印出数据,但是在csv文件的末尾,它还可以打印html页面代码!

Below is my code 下面是我的代码

$file="billing";
     $i=0;
     $values = mysql_query("SELECT * FROM billing");
     $rown=0;
     while( $row = mysql_fetch_assoc($values)){
     if($rown++==0)
       $csv_output.=implode(";",array_keys($row))."\n";
       $csv_output.=implode(";",array_values($row))."\n";
}

$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;
}

Output: 输出:

billing_id;customer_id;user_id;trans_id;transfer;balance;created;text;credit;sender_id      
257;29;;-1;0;500000;1330930434;Payment;500000;      
258;29;;-1;200000;300000;1330930465;Sender ID reg.;0;jkjjhh     
284;32;;-1;0;1000000;1331708884;Payment;1000000;        
285;32;564;268;-120;999880;1331709106;SMS send;;        
286;32;;-1;0;1000000;1331709234;Payment;120;        
287;32;564;269;0;1000000;1331723634;SMS send;;


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">       
    <html xmlns="http://www.w3.org/1999/xhtml">     
    <head>      
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />       
    <title>Client Manger</title>        
    <link rel="stylesheet" href="/style/my-style.css">      
    <form action='./' method='post'>        
    <input type="hidden" name="action" value="billing" class="text">        
    <input type="hidden" name="customer_id" value=""  />

What could be the reason? 可能是什么原因?

Thanks for all. 谢谢大家

You need to have exit; 您需要exit; or die(); die(); after the print $csv_output; print $csv_output; Otherwise the rest of the page (outside of that close } (which I suspect is the end of an if statement)) will print (and therefore be included in the CSV). 否则,页面的其余部分(该close之外} (我怀疑这是if语句的结尾)将被打印(因此包含在CSV中)。


Just a thought: it might be a better idea to have two PHP files to handle this: one which outputs the CSV, the other which outputs the form. 只是想一想:最好有两个PHP文件来处理此问题:一个输出CSV,另一个输出形式。 Not only allow you to avoid using die/exit or very long else statements it will also result in cleaner code overall. 不仅可以避免使用die / exit或使用else很长的语句,还可以总体上使代码更简洁。


As another thought, you are better off using do...while any time you want to have something happen the first time (and only the first time). 另一个想法是,最好在每次第一次(只有第一次)发生某件事时使用do...while So this might be a better option: 因此,这可能是一个更好的选择:

$row = mysql_fetch_assoc($values);
$csv_output.=implode(";",array_keys($row))."\n";
do
{
    // notice how there is no flag needed!
    // also, you don't need to call 'array_values' when imploding
    $csv_output.=implode(";",$row)."\n";
}while( $row = mysql_fetch_assoc($values));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM