【问题标题】:php generating csv file only works first timephp生成csv文件只在第一次工作
【发布时间】:2014-06-18 09:03:44
【问题描述】:

我有以下脚本:

<?php
//Define basepath for codeigniter
define('BASEPATH', '/');

//Include constants.php config file from codeigniter
require_once "../../my_manager/system/application/config/constants.php";

//Check username and password from GET
if( $_GET['username'] != IMPORT_USERNAME || $_GET['password'] != IMPORT_PASSWORD )
{
    header('HTTP/1.0 401 Unauthorized');
    echo "Denied Access";
    return; 
}

//running scripts
include 'export_table1.php';
include 'export_table2.php';
include 'export_table3.php';
include 'export_table4.php';
?>

每次导出如下:

<?php
//$host="localhost";
$host="";
$user="";
$pass="";
$db_name="";
$table="table1";
$conn = mysqli_connect($host,$user,$pass,$db_name) or die("Connection Error");

$query = "SELECT * FROM $table ORDER BY ID";
$result = mysqli_query($conn,$query) or die("sql error");
if(mysqli_num_rows($result)>0)
{
$csv = "";

$row = mysqli_fetch_assoc($result);

$delim = "";
//retrieving first line fields
foreach($row as $k => $v)
{
$csv .= $delim . '"' . str_replace('"', '""', $k) . '"';
$delim= ";";
}
$csv .= "\n";

//retrieving value into fields
while($row = mysqli_fetch_assoc($result))
{

$delim = "";
foreach($row as $v)
{
$csv .= $delim . '"' . str_replace('"', '""', $v) . '"';
$delim = ";";
}
$csv .= "\n";

}
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=".$table.".csv");
echo $csv;
exit
}
else
{
echo "No records";
}
 exit

?>

问题是只有第一个脚本被触发,所以我只能将第一个表下载到 csv 文件中。如果我首先启动 table2 导出,则只会触发 table2 导出。我该如何管理?提前致谢!

【问题讨论】:

  • 因为您在导出脚本的末尾exit...!?另外,您不能在一个响应中输出多个 CSV 文件,它们都会变成一个文件。

标签: php mysql csv export-to-csv


【解决方案1】:

据我所知,“退出”会杀死子脚本和父脚本 - 这就是为什么您只能在 csv 中导出第一个表的原因。请改用“return”继续。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    相关资源
    最近更新 更多