好吧,这是我正在努力将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";
}
}
?>
答案 0 :(得分:0)
我不确定您是否可以直接将数据导出到.CSV
,但如果只是将其替换为.XLS
,则代码工作正常。然后,您可以将其转换为.CSV
。希望这种方式有效
$filename = 'text.xls';