Php 如何使用连接列导出到csv
我想将mysql数据导出到csv文件中,包含concat 4列,但它不起作用。有人能尽快帮我吗 我想把Add1,Add2,Add3,City,Pincode这些列合并起来,它将在csv文件中的单个列中显示出来 提前通知 我的PHP代码如下Php 如何使用连接列导出到csv,php,mysql,excel,csv,Php,Mysql,Excel,Csv,我想将mysql数据导出到csv文件中,包含concat 4列,但它不起作用。有人能尽快帮我吗 我想把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
<?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']},";
像这样改变
$contents.=$row['Add1'].$row['Add2'].$row['Add3'].",";
将您的查询更改为
SELECT Sr_no,Entry_Date,Agent_Name,Process_Name,Donar_Name,
CONCAT_WS(" ", Add1,Add2,Add3,City,PinCode) AS Full_Address,
Mobile_no,Nearest_station,Suburbans,Pickup_time,Confirmation_Status,
Pickup_Amount,Field_Executive FROM leads where Entry_Date='$dt'");
那么,最好使用fputcsv,而不是自己为输出构建字符串
header("Content-Disposition: attachment; filename=Leads For".date('d-m-Y').".csv");
$fp = fopen("php://output", "w");
fputcsv($fp, array("SR NO","DATE","AGENT NAME","PROCESS","DONAR NAME","ADDRESS","CONTACT NO","NEAREST STATION","SUBURBANS","PICKUP TIME","CONFIRMATION STATUS","PICKUP AMOUNT","FIELD EXECUTIVE"));
while($row = mysql_fetch_array($user_query))
{
// strip tags out of the field
$row = array_map("strip_tags", "row");
fputcsv($fp, $row);
}
fclose($fp);
您好,我可以用select query$user\u query=mysql\u query进行更改吗(“选择Sr\u编号、输入日期、代理名称、流程名称、Donar\u名称、Add1、Add2、Add3、移动\u编号、最近的\u站、郊区、取货时间、确认状态、取货金额、输入日期为“$dt”的Lead中的字段执行人员”);对你可以改变。您必须删除此行$contents.=addslashes($row['Full_Address']);如何使用select query$user\u query=mysql\u query进行更改(“选择Sr\u编号、输入日期、代理名称、流程名称、Donar\u名称、Add1、Add2、Add3、移动\u编号、最近的\u车站、郊区、取货时间、确认状态、取货金额、输入日期为“$dt”的Lead中的字段执行人员”);好吧,我误解你了。。我认为您希望连接PHP变量,而不是从SQL查询返回连接的值。在这种情况下,@Orangepill的答案是正确的。你好,尼克,根据你的建议,我做了更改,但它不起作用,你能帮我吗?你好,根据你的建议,我做了更改,但它不起作用。还有别的办法吗。