使用fputcsv连接列

时间:2019-05-07 02:22:05

标签: php mysql excel csv

我有一个程序,用户可以在其中从数据库下载所有查询值的csv文件。但是,在我的一列中,我需要将单词“” Serial Number-“和我的m.sr_no值合并在一起。怎么可能呢?

if(isset($_POST["Export"])){
		 
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=Sample.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('SR_NO', 'MODEL', 'EWR_NO',  'BUILD_NAME',  'SA_MB', 'DESTINATION', 'REQUESTOR', 'PURPOSE', 'WAFERNUM', 
		'CELL', 'QTY', 'MATERIAL_FORM', 'SAMPLE_CRITERIA', 'REQUEST_DATE', 'SORTING_ECD', 'HGA_ECD', 'STATUS', 'UPDATED_BY', 'REMARKS'));  
     $query = "SELECT m.sr_no, a.model, m.ewr_no, m.build_name, m.sa_mb, a.destination, m.requestor, m.purpose, m.wafernum, 
				m.cell, m.qty, a.material_form, a.criteria, m.request_date, m.sorting_ecd, m.hga_ecd, m.status, m.updated_by, m.remarks, m.id_sorting, a.id_optionlist
				FROM sorting  m
				INNER JOIN dynamic_optionlist a ON m.id_sorting = a.id_optionlist 
				WHERE STATUS in ('Open','On-Going','On-Queue','Done')";
      $result = mysqli_query($conn, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {  
           fputcsv($output, $row);  
      }  
      fclose($output);  
 }  

2 个答案:

答案 0 :(得分:1)

您可以使用MySQL的CONCAT函数在查询中进行合并:

$query = "SELECT CONCAT('Serial Number - ', m.sr_no) AS sr_no, ...";

答案 1 :(得分:1)

解决方案1:

将您的while循环替换为以下内容:

 while($row = mysqli_fetch_assoc($result))  
 {  
   $row['m.sr_no'] = "Serial Number-".$row['m.sr_no'];
   fputcsv($output, $row);  
 } 

解决方案2:

按照提到的nick更新查询:

CONCAT('Serial Number - ', m.sr_no) AS sr_no,