最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - export a mysql table to csv with column headers

programmeradmin2浏览0评论

I am able to download a file in csv format for my table , but how to add column headers to the same file .

The current code is following -

// load wpdb
                        $path = $_SERVER['DOCUMENT_ROOT'];
                        include_once $path . '/wp-load.php';

                         global $wpdb;

                       $table = $_POST["table_name"];// table name
                       $file = 'database_csv'; // csv file name
                       $results = $wpdb->get_results("SELECT * FROM $wpdb->prefix$table",ARRAY_A );

                       if(count($results) > 0){
                          foreach($results as $result){
                          $result = array_values($result);
                          $result = implode(", ", $result);
                          $csv_output .= $result."\n";
                        }
                      }

                      $filename = $file."_".date("Y-m-d_H-i",time());
                      header("Content-type: application/vnd.ms-excel");
                      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                      header( "Content-disposition: filename=".$filename.".csv");
                      header("Pragma: no-cache");
                      header("Expires: 0");
                      print $csv_output;
                      exit;

I am able to download a file in csv format for my table , but how to add column headers to the same file .

The current code is following -

// load wpdb
                        $path = $_SERVER['DOCUMENT_ROOT'];
                        include_once $path . '/wp-load.php';

                         global $wpdb;

                       $table = $_POST["table_name"];// table name
                       $file = 'database_csv'; // csv file name
                       $results = $wpdb->get_results("SELECT * FROM $wpdb->prefix$table",ARRAY_A );

                       if(count($results) > 0){
                          foreach($results as $result){
                          $result = array_values($result);
                          $result = implode(", ", $result);
                          $csv_output .= $result."\n";
                        }
                      }

                      $filename = $file."_".date("Y-m-d_H-i",time());
                      header("Content-type: application/vnd.ms-excel");
                      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                      header( "Content-disposition: filename=".$filename.".csv");
                      header("Pragma: no-cache");
                      header("Expires: 0");
                      print $csv_output;
                      exit;
Share Improve this question asked Jun 28, 2017 at 13:43 Divyanshu JimmyDivyanshu Jimmy 4493 gold badges9 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You should build Your columns schemat in array. Than add this on the top of csv and than You can add content of Your csv.

$string_headers = 'post_title,post_author,etc....';

if(count($results) > 0)
{
    $result = $string_header;
    foreach($results as $result){
    $tmp_result = array_values($result);
    $result .= implode(", ", $tmp_result);
    $csv_output .= $result."\n";
}

I was able to do it by first getting column names and then assigning it to final output :

  $table_name = $wpdb->prefix.$_POST["table_name"];// table name
                       $file = 'database_csv'; // csv file name
                       $results = $wpdb->get_results("SELECT * FROM $table_name",ARRAY_A );

                        // get column names
                        $query = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='".$wpdb->dbname."' AND `TABLE_NAME`='".$table_name."'";

                        $columnNamesList = $wpdb->get_results($query);  


                        foreach ( $columnNamesList as $column_name ) {
                            $csv_output.=$column_name->COLUMN_NAME.",";
                        }


                        // remove last additional comma 
                        $csv_output = substr($csv_output,0,strlen($csv_output)-1);

                        // start dumping csv rows in new line
                        $csv_output.="\n";

                       if(count($results) > 0){
                          foreach($results as $result){
                          $result = array_values($result);
                          $result = implode(", ", $result);
                          $csv_output .= $result."\n";
                        }
                      }

                      $filename = $file."_".date("Y-m-d_H-i",time());
                      header("Content-type: application/vnd.ms-excel");
                      header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                      header( "Content-disposition: filename=".$filename.".csv");
                      header("Pragma: no-cache");
                      header("Expires: 0");
                      print $csv_output;
                      exit;
发布评论

评论列表(0)

  1. 暂无评论