douchen4915 2012-12-11 11:52
浏览 48

从MySQL表创建一个tsv文件。 添加更多虚构的标题

I have a MySQL table, from which I create tsv and csv files. I want to create the tsv file whilst adding imaginary headers to the file. I already use the MySQL column headers as the headers for the file, but I need to add extra imaginary headers not in the MySQL table. My current code creates the file, but I do not know how to go about adding the imaginary headers.

It outputs

name    age address
Daniel  24  Carlifornia
Jane    22  New York

I want to output

name    age address option1 option2
Daniel  24  Carlifornia anything    anything
Jane    22  New York    anything    anything

Here's my code:

@chmod($export_tsv, 0777);
$fe = @fopen($export_tsv."/export.tsv", "w+");
if($fe){           
    $somecontent = "";
    //$somecontent = "header('Content-type: text/html; charset=utf-8')";
    $fields_count = 0;

    // fields headers
    $db->query($sql_view);
    if($row = $db->fetchAssoc()){
        foreach($row as $key => $val){
            if($fields_count++ > 0) $somecontent .= "\t";
            // mysql column headers here
            $somecontent .= $key;
        }
    }
    $somecontent .= "
"; 

    $db->query($sql_view);
    while($row = $db->fetchAssoc()){
        $fields_count = 0;
        foreach($row as $key => $val){
            if($fields_count++ > 0) $somecontent .= "\t";
            //my own special code start
            $val = str_replace("
","", $val);
            $val = str_replace("","", $val);
            $val = str_replace("\t","", $val);

            $val = stripslashes($val);                    
            $val = str_replace("chr(13)","", $val);
            //my own special code end

            $somecontent .= $val;              
        }
        $somecontent .= "
"; 
    }
    utf8_encode($somecontent);
    $somecontent = mb_convert_encoding($somecontent, 'HTML-ENTITIES', "UTF-8");

    // write some content to the opened file.
   if (fwrite($fe, utf8_encode($somecontent)) == FALSE)
       echo 'file_writing_error'." (export.tsv)"; 
   fclose($fe);           
}
  • 写回答

1条回答 默认 最新

  • duanbi1983 2012-12-11 11:55
    关注

    Maybe you can use the AS keyword in your SQL query?

    select something AS `new column name` FROM some_table
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题