Php can';t将sql查询输出保存为.csv格式
我面临sql查询问题,请有人帮助我 在我的管理面板中,几乎没有下载数据的选项。csv格式,通过使用数据库中的sql查询获得。所有查询都是单一查询,可以成功下载.csv格式的数据,除了Sql查询之后的。我无法以.csv格式下载此查询结果 这就是为什么我要问这个问题,如何将这个查询输出(有两个while语句和long查询)保存为.csv格式?现在不需要将结果保存为.csv格式 单个查询,如Php can';t将sql查询输出保存为.csv格式,php,mysql,Php,Mysql,我面临sql查询问题,请有人帮助我 在我的管理面板中,几乎没有下载数据的选项。csv格式,通过使用数据库中的sql查询获得。所有查询都是单一查询,可以成功下载.csv格式的数据,除了Sql查询之后的。我无法以.csv格式下载此查询结果 这就是为什么我要问这个问题,如何将这个查询输出(有两个while语句和long查询)保存为.csv格式?现在不需要将结果保存为.csv格式 单个查询,如 $sql = mysql_query("select * from user_property upr, o
$sql = mysql_query("select * from user_property upr, offers ofr where ofr.property_id
= upr.property_id and ofr.agent_id IN (select id from users where company_name !='') order
by accept_ind asc");
Sql查询:
<?php
// Database Connection
$host="host";
$uname="username";
$pass="password";
$database = "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>");
$output = "`";
$q = mysql_query("SELECT * FROM users WHERE company_name != '' ");
while($r= mysql_fetch_array($q)){
$user_id = $r['id'];
$pc1 = $r['pc1'];
$pc2 = $r['pc2'];
$pc3 = $r['pc3'];
$pc4 = $r['pc4'];
$emailAgent = $r['user_email'];
$sql = mysql_query("
SELECT *
FROM user_property upr
WHERE (postcode = '$pc1' OR
postcode = '$pc2' OR
postcode = '$pc3' OR
postcode = '$pc4') AND
datediff(CURDATE(), upr.creation_date) <= 7 AND
NOT EXISTS(SELECT ofr.property_id
FROM offers ofr
WHERE ofr.property_id = upr.property_id AND
ofr.agent_id IN(SELECT id
FROM users
WHERE company_name !=''
)
)
ORDER BY property_id DESC");
while($rrr = mysql_fetch_array($sql)){
$columns_total = mysql_num_fields($sql);
// Get The Field Name
for ($i = 0; $i < $columns_total; $i++) {
$heading = mysql_field_name($sql, $i);
$output .= '"'.$heading.'",';
}
$output .="\n";
// Get Records from the table
while ($row = mysql_fetch_array($sql)) {
for ($i = 0; $i < $columns_total; $i++) {
$output .='"'.$row["$i"].'",';
}
$output .="\n";
}
// Download the file
$filename = "myFile.csv";
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
echo $output;
exit;
}// second while
}/// first while...
?>
PHP内置了创建CSV文件的函数。您应该正在使用它们。可能重复使用此代码的@johncode我可以使用一个查询创建CSV文件,但无法使用2使用while语句查询(Sql查询)
创建CSV文件。