将MySQL表导出为带有列标题的CSV

时间:2017-06-28 作者:Divyanshu Jimmy

我可以为我的表下载csv格式的文件,但是如何将列标题添加到同一文件中。

当前代码如下-

// 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;

2 个回复
SO网友:Sebastian Kurzynowski

您应该在数组中构建列架构。然后将其添加到csv的顶部,然后您可以添加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";
}

SO网友:Divyanshu Jimmy

我能够做到这一点,首先获取列名,然后将其分配给最终输出:

  $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;

结束

相关推荐

非登录用户的函数.php中的Author Page用户ID

我想访问作者id 当访问者访问作者页面时?当我输入代码时author.php 文件,我可以像下面这样打印作者id$q_obj = get_queried_object(); $user_id = (int) $q_obj->data->ID; 但是,如果我想user id 功能。php,我应该如何打印?function zb_user_analyze() { // I would like to access user id here } ad