日韩天天综合网_野战两个奶头被亲到高潮_亚洲日韩欧美精品综合_av女人天堂污污污_视频一区**字幕无弹窗_国产亚洲欧美小视频_国内性爱精品在线免费视频_国产一级电影在线播放_日韩欧美内地福利_亚洲一二三不卡片区

php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法_PHP教程

編輯Tag賺U幣
教程Tag:暫無Tag,歡迎添加,賺取U幣!

推薦:php輸出全球各個時區(qū)列表的方法
具體實現(xiàn)方法如下:

這篇文章主要介紹了php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法,涉及php查詢mysql及操作csv文件的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了php使用指定編碼導(dǎo)出mysql數(shù)據(jù)到csv文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:

  1. <?php 
  2. /* 
  3.  * PHP code to export MySQL data to CSV 
  4.  *  
  5.  * Sends the result of a MySQL query as a CSV file for download 
  6.  * Easy to convert to UTF-8. 
  7.  */ 
  8.   
  9.  /* 
  10.  * establish database connection 
  11.  */ 
  12.   
  13. $conn = mysql_connect('localhost''login''pass'or die(mysql_error()); 
  14. mysql_select_db('database_name'$connor die(mysql_error($conn)); 
  15. mysql_query("SET NAMES CP1252"); 
  16. /*  
  17.  * execute sql query    
  18.  */ 
  19. $query = sprintf('SELECT field1,field2 FROM table_name'); 
  20. $result = mysql_query($query$connor die(mysql_error($conn)); 
  21. /*  
  22.  * send response headers to the browser 
  23.  * following headers instruct the browser to treat the data as a csv file called export.csv 
  24.  */ 
  25. header('Content-Type: text/csv; charset=cp1252'); 
  26. header('Content-Disposition: attachment;filename=output.csv'); 
  27. /*  
  28.  * output header row (if atleast one row exists)  
  29.  */ 
  30.     
  31. $row = mysql_fetch_assoc($result);  
  32. if ($row) { 
  33.   echocsv(array_keys($row)); 
  34.   
  35. /* 
  36.  * output data rows (if atleast one row exists) 
  37.  */ 
  38. while ($row) { 
  39.   echocsv($row); 
  40.   $row = mysql_fetch_assoc($result); 
  41.   
  42. /* 
  43.  * echo the input array as csv data maintaining consistency with most CSV implementations 
  44.  * - uses double-quotes as enclosure when necessary 
  45.  * - uses double double-quotes to escape double-quotes 
  46.  * - uses CRLF as a line separator 
  47.  */ 
  48.   
  49. function echocsv($fields
  50.   $separator = ''
  51.   foreach ($fields as $field) { 
  52.     if (preg_match('/\\r|\\n|,|"/'$field)) { 
  53.  $field = '"' . str_replace('"''""'$field) . '"'
  54.     } 
  55.     echo $separator . $field
  56.     $separator = ','
  57.   } 
  58.   echo "\r\n"
  59. ?> 

分享:php限制ip地址范圍的方法
只有在限定范圍內(nèi)的ip地址才能訪問 希望本文所述對大家的php程序設(shè)計有所幫助。

來源:模板無憂//所屬分類:PHP教程/更新時間:2015-04-01
相關(guān)PHP教程