douzhantao2857 2014-08-25 12:38
浏览 66
已采纳

使用php从mysql数据库导出csv文件

<?php
    include("includes/config.php"); 
    if ($_POST['frmCsv']) {
        $strSelect = doSelectCsv();
        header( 'Content-Type: text/csv' );
        header( 'Content-Disposition: attachment;filename=export.csv' );
        $strRow = mysql_fetch_assoc( $strSelect );
        printArray($strRow);
        if ( $strRow ) {
            outputcsv( array_keys( $strRow ) );
        }
        while ( $strRow ) {
            outputcsv( $strRow ); 
        }
    }

    function doSelectCsv()
    {
        $strSql = "SELECT * FROM tbl_member";
        $strResult = SelectQry($strSql);
    }

    function outputcsv( $fields )
    {
        $separator = '';
        foreach ( $fields as $field ) {
            echo $separator . $field;
            $separator = ',';     
        }
    }
?>

This is my code to export a csv file from the database. but it could not select the datas. select query did not works correctly. please help me.

  • 写回答

2条回答 默认 最新

  • drxkx6149 2014-08-26 08:33
    关注

    Just use it brother :)

    <?php
    
    if($_POST["download"]){ 
            $output="";
            $line_termineted="
    ";
    
            if( $_POST["download"] =="CSV") $field_termineted=","; else $field_termineted="\t"; 
                $enclosed='"';
                $escaped="\\";
    
                $export_schema="SR No".$field_termineted."Student ID".$field_termineted."First Name".$field_termineted."Middle Name".$field_termineted."Last Name";
                $dataQuery=mysql_query("select * from sof_student ");
                $output.=$export_schema;
                $p=0;
                while($data=mysql_fetch_array($dataQuery)) {
                 $p++;
                    $output.= $line_termineted.$p.$field_termineted;
                    $output.=$enclosed.$data["id"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["first_name"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["middle_name"].$enclosed.$field_termineted;
                    $output.=$enclosed.$data["last_name"].$enclosed.$field_termineted;
                  }
    
        header("Content-Description: File Transfer");
       if( $_POST["download"] =="CSV"){
            header("Content-Type: application/csv");
            header("Content-Disposition: attachment; filename=report".date("d_m_Y_H_i_s").".csv");
        } else {
            header("Content-Type: application/vnd.ms-excel");
            header("Content-disposition: attachment; filename=report".date("d_m_Y_H_i_s").".xls");
        }
    
        header("Content-Transfer-Encoding: binary");
        header("Expires: 0");
        header("Cache-Control: must-revalidate");
        header("Pragma: public");
        header("Content-Length: ".strlen($output));
        ob_clean();
        flush();
        echo $output;
        exit;
    }
     ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退