在CSV文件中\ t(tab)不接受使用php作为列分隔符

时间:2014-03-06 06:57:15

标签: php mysql

好吧,这是我正在努力将mysql查询结果保存为.csv格式的文件。它成功返回数据,但当我查看CSV文件时,它会在每个?中显示row标记。这是因为它不接受\t制表符作为列分隔符来分隔每个列值。

如何在CSV中使用t字符,如何在每个列中按行显示结果不将每列值覆盖到另一列?

我在CSV文件中的期望如下:

--------------------------
Column 1        Column 2
-------------------------
result 1       result 2
result 1       result 2
result 1       result 2
result 1       result 2 

但它显示:http://propertyjungle.com.au/correct-view.gif

csv.php文件

?php
$filename = 'text.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

$host="";
$uname="";
$pass="";
$database = ""; 
$connection=mysql_connect($host,$uname,$pass); 
echo mysql_error();

//or die("Database Connection Failed");
$selectdb=mysql_select_db($database) or die("Database could not be selected");  
$result=mysql_select_db($database)
or die("database cannot be selected <br>");

$sqlagentdetails = "select * from users WHERE company_name != ''";
$rowresult = mysql_query($sqlagentdetails); 

echo "Column 1";
echo "\t";
echo "Column 2";
echo "\n";


while($row = mysql_fetch_array($rowresult, MYSQL_ASSOC))
{
    $myQuery =  mysql_query( /* query are goes to here...*/ );

    while($row = mysql_fetch_array($myQuery)){  

    $postcode = "{$row['postcode']}";  
    $paddress = "{$row['paddress']}";

echo "$emailAgent";
echo "\t";
echo "$paddress";
echo "\n";

    }
}
?>

1 个答案:

答案 0 :(得分:0)

我不确定您是否可以直接将数据导出到.CSV,但如果只是将其替换为.XLS,则代码工作正常。然后,您可以将其转换为.CSV。希望这种方式有效

$filename = 'text.xls';