1
我有一個MySQL表,從中創建tsv和csv文件。我想創建tsv文件,同時添加虛構的頭文件。我已經使用MySQL列標題作爲文件的標題,但是我需要添加額外的虛構標題而不是MySQL表格。我目前的代碼創建該文件,但我不知道如何去添加虛構的頭文件。從MySQL表創建一個tsv文件。添加更多虛構的頭文件
它輸出
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);
}
謝謝。正在尋找一個機會,讓它在飛行中打印而不用更改或使用代碼 – karto
中的select語句。這就是您在查詢中「重命名」結果列的方法,我認爲這就是您想象中的虛列。特別是因爲該列的價值可能是真的。你想要做的是什麼?你可以用你當前輸出的內容和你想輸出的內容來更新你的問題嗎? – Svish