简体   繁体   中英

Export PHP CSV File Columns not displayed

The code below exports data from mysql table correctly but the column names are not displayd in the outputted csv file. Please help

if(isset($_POST["export"])){
    $branchcode=$_POST['branchcode'];
    $start=$_POST['start'];
    $end=$_POST['end'];
    $sql=mysqli_query($conn,"select tk_file,tk_date,userid,tk_from,tk_to from rfc_timekeeping_history where branchcode='$branchcode' and (tk_date between '$start' and '$end');")or die(mysqli_error());
    $output = "";
    $columns_total =    mysqli_num_fields($sql);
    for ($i = 0; $i < $columns_total; $i++) {
    $heading =  mysqli_fetch_fields($sql, $i);
    $output .= '"'.$heading.'",';
    }
    $output .="\n";
    while ($row = mysqli_fetch_array($sql)) {
    for ($i = 0; $i < $columns_total; $i++) {
    $output .='"'.$row["$i"].'",';
    }
    $output .="\n";
    }
    $filename = "myFile.csv";
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename);
    echo $output;
    exit;
}

A different approach using the standard php methods of writing to a csv file ( fputcsv ) and using stdout stream you could try along these lines. The column names are obtained in the first loop before rewinding the recordset to actually process the recordset data.

<?php

    if( isset( $_POST["export"], $_POST['branchcode'], $_POST['start'], $_POST['end'] ) ){

        $filename = "myFile.csv";
        $branchcode=$_POST['branchcode'];
        $start=$_POST['start'];
        $end=$_POST['end'];



        /* store columns names in this array */
        $headers=array();

        /* Run the query */
        $sql = mysqli_query( $conn, "select `tk_file`,`tk_date`,`userid`,`tk_from`,`tk_to` from `rfc_timekeeping_history` where `branchcode`='{$branchcode}' and ( `tk_date between` '{$start}' and '{$end}');")or die(mysqli_error());

        /* fetch the fields */
        $fields = mysqli_fetch_fields( $sql );

        /* add each column name to the array */
        foreach( $fields as $field ) {
            $headers[]=$field->name;
        }


        /* establish basic csv formatting parameters */
        $delimiter=',';
        $enclosure='"';

        /* write output to stdout */
        $output=fopen('php://output','w+');
        fputcsv( $output, $headers, $delimiter, $enclosure );

        /* rewind the recordset to begin processing data */
        mysqli_data_seek( $sql, 0 );

        /* process each row - add to stdout */
        while( $row = mysqli_fetch_array( $sql ) ) {
            fputcsv( $output, $row, $delimiter, $enclosure );
        }

        /* close stdout stream */
        fclose( $output );

        /* send the data */
        header("Content-Type: application/csv");   
        header("Content-Disposition: attachment; filename={$filename}");  
        ob_flush();
        exit();
    }

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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