Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySQL表创建tsv文件。添加更多虚构的标题_Php_Csv_Export To Csv - Fatal编程技术网

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

Php 从MySQL表创建tsv文件。添加更多虚构的标题,php,csv,export-to-csv,Php,Csv,Export To Csv,我有一个MySQL表,我从中创建tsv和csv文件。我想创建tsv文件,同时向文件添加虚构的头。我已经使用MySQL列标题作为文件的标题,但是我需要添加额外的虚构标题,而不是MySQL表中的标题。我当前的代码创建了该文件,但我不知道如何添加虚构的头 它输出 name age address Daniel 24 Carlifornia Jane 22 New York 我想要输出 name age address option1 option2 Daniel 24 C

我有一个MySQL表,我从中创建tsv和csv文件。我想创建tsv文件,同时向文件添加虚构的头。我已经使用MySQL列标题作为文件的标题,但是我需要添加额外的虚构标题,而不是MySQL表中的标题。我当前的代码创建了该文件,但我不知道如何添加虚构的头

它输出

name    age address
Daniel  24  Carlifornia
Jane    22  New York
我想要输出

name    age address option1 option2
Daniel  24  Carlifornia anything    anything
Jane    22  New York    anything    anything
这是我的密码:

@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 .= "\r\n"; 

    $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("\n","", $val);
            $val = str_replace("\r","", $val);
            $val = str_replace("\t","", $val);

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

            $somecontent .= $val;              
        }
        $somecontent .= "\r\n"; 
    }
    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);           
}
也许您可以在SQL查询中使用AS关键字

select something AS `new column name` FROM some_table

谢谢我们正在寻找一个在不更改代码或使用代码中的select语句的情况下即时打印的机会这就是在查询中动态重命名结果列的方式,我认为这就是您所说的虚拟列。特别是因为该列的值实际上可以是任何值。你真正想做的是什么?你能不能用一个例子来更新你的问题,说明你当前输出的内容和你想输出的内容?