php生成csv文件只能在第一次使用

时间:2014-06-18 09:03:45

标签: php mysql csv export-to-csv

我有以下脚本:

<?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';
?>

Eache导出如下:

<?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导出。我该如何管理?提前谢谢!

1 个答案:

答案 0 :(得分:2)

据我所知&#34;退出&#34;杀死子脚本和父脚本 - 这就是为什么你只获得在csv中导出的第一个表的原因。使用&#34;返回&#34;而是继续。