Php 将查询打印为csv时出现问题

Php 将查询打印为csv时出现问题,php,export-to-csv,Php,Export To Csv,我这里有这个查询,这是我输出到csv的方式,有人能帮我吗? 那里很乱,如果我把它打印成Excel会更好,我尝试了一些版本,但是我使用odbc查询,这让我很难将它与我在网上找到的示例相适应 $result = "SELECT forma.*, SMS_MONTIME.IDTICKET,SMS_MONTIME.MBYLLUR, SMS_MONTIME.time_added FROM forma LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTI

我这里有这个查询,这是我输出到csv的方式,有人能帮我吗? 那里很乱,如果我把它打印成Excel会更好,我尝试了一些版本,但是我使用odbc查询,这让我很难将它与我在网上找到的示例相适应

$result = "SELECT forma.*, SMS_MONTIME.IDTICKET,SMS_MONTIME.MBYLLUR,
SMS_MONTIME.time_added FROM forma 
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE 
forma.data_fillim >= '$df' AND forma.data_fillim <= '$dm' ORDER BY forma.id DESC"; 
$user_query = odbc_exec($connection, $result) or die(odbc_error());
//While loop to fetch the records
while($row = odbc_fetch_array($user_query))
{
$contents.=$row['klienti'].",";
$contents.=$row['id'].",";
$contents.=$row['tekniku_emer'].",";
$contents.=$row['telefoni'].",";
$contents.=$row['data_fillim'].",";
$contents.=$row['difekti'].",";
$contents.=$row['tekniku_mesazh'].",";
$contents.=$row['shenime'].",";

}

// remove html and php tags etc.
$contents = strip_tags($contents); 

//header to make force download the file
header("Content-Disposition: attachment; filename=Kondicioner".date('d-m-Y').".csv");
print $contents;
$result=“选择格式。*,SMS\u MONTIME.IDTICKET,SMS\u MONTIME.MBYLLUR,
SMS_MONTIME.time_从forma添加
在forma.ID=SMS\u MONTIME.IDTICKET上左键加入SMS\u MONTIME,其中

forma.data_fillim>=“$df”和forma.data_fillim输出csv的更好方法是使用fputcsv函数

<?php
$result = "SELECT forma.*, SMS_MONTIME.IDTICKET,SMS_MONTIME.MBYLLUR,
SMS_MONTIME.time_added FROM forma 
LEFT JOIN SMS_MONTIME ON forma.ID = SMS_MONTIME.IDTICKET WHERE 
forma.data_fillim >= '$df' AND forma.data_fillim <= '$dm' ORDER BY forma.id DESC"; 
$user_query = odbc_exec($connection, $result) or die(odbc_error());
//While loop to fetch the records
header("Content-Disposition: attachment; filename='Kondicioner".date('d-m-Y').".csv'");
$fp = fopen("php://output","w");
// here you set up your header
fputcsv($fp, array("klienti","id","tekniku_emer","telefoni","data_fillim","difekti","tekniku_mesazh", "shenime");
while($row = odbc_fetch_array($user_query))
{
   $row = array_map("strip_tags", $row);
   fputcsv($fp, array($row['klienti'],$row['id'],
                      $row['tekniku_emer'],$row['telefoni'],
                      $row['data_fillim'],$row['difekti'],
                      $row['tekniku_mesazh'],$row['shenime']));

}
fclose($fp);

除了使用PHP的内置函数将每一行写入CSV文件行会容易得多之外……你到底有什么问题?问题是,当我打开CSV时,没有任何东西在它所在的位置,行被弄乱了,它们没有分开……你没有在每一行的末尾放一个返回值……使用fputcsv()您不必担心这个问题,也不必担心从数据库中检索到的包含逗号的列,也不必担心任何其他可能出现的问题—您没有检查Hanks以获得答案,但我得到了以下错误:(!)警告:array_map()在E:\wamp\www\montim\montime bcp\montime\vendosja porosise\download all\index.php第28行Call stack抱歉…这是strip\u tags而不是striptagsBetter this
头(“内容处置:附件;文件名='kondiconer.date('d-m-Y'))(“.csv”)
filename括在quotes中太好了,谢谢:)如果我不介意,你能告诉我,我如何格式化它,使我有一个标题和信息在列中…而不需要导入文件来实现这一点..@Alb..刚刚做了…更多的是一样的…你可以将调用中的值更改为更人性化:)