php - 如何使用连接列导出到 csv

标签 php mysql excel csv

我想将 mysql 数据导出到包含 4 列的 csv 文件,但它不起作用,任何人都可以尽快帮助我吗? 我想连接 Add1、Add2、Add3、City、Pincode 这些列,它将显示在 csv 文件中的单列中

提前致谢

我的PHP代码如下

<?php

require_once("config.php");



//Enter the headings of the excel columns
$contents="SR NO,DATE,AGENT NAME,PROCESS,DONAR NAME,ADDRESS,CONTACT NO,NEAREST STATION,SUBURBANS,PICKUP TIME,CONFIRMATION STATUS,PICKUP AMOUNT,FIELD EXECUTIVE\n";

//Mysql query to get records from datanbase
//You can customize the query to filter from particular date and month etc...Which will depends your database structure.
$Sr_no=$_GET['Sr_no'];
$dt=date('Y-m-d');

$user_query = mysql_query("SELECT Sr_no,Entry_Date,Agent_Name,Process_Name,Donar_Name,CONCAT( IFNULL( Add1,  '' ) , ' ', IFNULL( Add2,  '' ) , ' ', IFNULL( Add3,  '' ),' ',IFNULL( City,  '' ),'', IFNULL( Pincode,  '' ) ) AS Full_Address,Mobile_no,Nearest_station,Suburbans,Pickup_time,Confirmation_Status,Pickup_Amount,Field_Executive FROM leads where Entry_Date='$dt'");

//While loop to fetch the records
while($row = mysql_fetch_array($user_query))
{

$contents.=$row['Sr_no'].",";
$contents.=$row['Entry_Date'].",";
$contents.=$row['Agent_Name'].",";
$contents.=$row['Process_Name'].",";
$contents.=$row['Donar_Name'].",";
$contents.=addslashes($row['Full_Address']).",";
//$contents.=$row[{['Add1']}{$row['Add2']}{$row['Add3']}].",";
$contents.=$row['Mobile_no'].",";
$contents.=$row['Nearest_station'].",";
$contents.=$row['Suburbans'].",";
$contents.=$row['Pickup_time'].",";
$contents.=$row['Confirmation_Status'].",";
$contents.=$row['Pickup_Amount'].",";
$contents.=$row['Field_Executive']."\n";

}



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

//header to make force download the file
header("Content-Disposition: attachment; filename=Leads For".date('d-m-Y').".csv");
print $contents;



?>

最佳答案

更改此:

$contents.=$row[{['Add1']}{$row['Add2']}{$row['Add3']}].",";

对此:

$contents.="{$row['Add1']}{$row['Add2']}{$row['Add3']},";

关于php - 如何使用连接列导出到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963914/

相关文章:

php - do_shortcode 不起作用

php - SQL, "WHERE IN"返回自动排序结果

mysql - 外键冗余

mysql - 左连接的慢速选择查询为空并限制结果

asp.net - Create new sql entry with one value being MAX(Column)+1 给出 Invalid use of group function

excel - Excel for Mac 2016 是否可以正确导入 .csv 文件中的 Unicode?

excel - 如何计算存储在excel单个单元格中的数组的模式?

php - MySQL 模组与 PHP 模组

php - mysql 检查帐户类型以查看登录时是否为 admin

excel - 在 vba 用户窗体中使用不同的背景颜色