export data from mySQL to csv

 怎麼把數據送MySQL database export to csv? N=o~/%Wz|  
jsp VUQ4  
非常簡單: QQ:` {r  
8!weV  
<?php p17YBf�C  
// Connect database %rfZ2HD R4  
$database="yourdatabase"; ^)kuiU!y  
$table="yourtable"; uf<k6|-}fJ  
mysql_connect("localhost","username","password"); f$'_uv7o  
mysql_select_db("yourdatabase"); r s-( ]@|%  
$result=mysql_query("select * from $table"); Z' iD_mtZ  
w52Y�}A $  
$out = ''; <Kq;k^XdN  
j3@&/;W9>  
// Get all fields names in table "yourtable" in database "yourdatabase". ?#nT.  
$fields = mysql_list_fields(yourdatabase,$table); %`Y5I+b  
%XtSaz/]Y  
// Count the table fields and put the value into $columns. /O[C�=g  
$columns = mysql_num_fields($fields); [q*/O~*0  
0:'H �(n  
5`#>^/p9  
// Put the name of all fields to $out. M!jTe3HKJ~  
for ($i = 0; $i < $columns; $i++) { G#pU,oL]1K  
$l=mysql_field_name($fields, $i); M qv VC1  
$out .= '"'.$l.'",'; yUn(= U5  
} aN l2r  
$out .="/n"; r'0+~  
/p!<bK+  
// Add all values in the table to $out. gTK 8  
while ($l = mysql_fetch_array($result)) { Ks*xm1l3  
for ($i = 0; $i < $columns; $i++) { &>d}1Y/9 i  
$out .='"'.$l["$i"].'",'; E{NCyf2Jo  
} d}dWO  
$out .="/n"; >> Dc  
} 7$lpq@:0  
o1v@J<#,  
// Open file export.csv. -g1i_<5:  
$f = fopen ('export.csv','w'); G eS6Kf  
#=VYKND/  
// Put all values from $out to export.csv. $82/y2  
fputs($f, $out); G)BhV  
fclose($f); sE[]r;Bq[  
g*QIx?Z  
header('Content-type: application/csv'); [e@s{qb  
header('Content-Disposition: attachment; filename="export.csv"'); 2 TspT,t  
#readfile('export.csv'); >>62v0w2  
?> P#(<w)(,   
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章