开发者

Allow users to pull temporary data then delete table data (headers remain)?

I don't know the best way to title this question but am trying to accomplish the following goal:

When a client logs into their profile, they are presented with a link to download data from an existing database in CSV format. The process works, however, I would like for this data to be 'fresh' each time they click the link so my plan was - once a user has clicked the link and downloaded the CSV file, the database table would 'erase' all of its data and start fresh (be empty) until the next set of data populated it.

My EXISTING CSV creation code:

<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'database';
$table = 'tablename';
$file = 'export';



$link = mysql_connect($host, $user, $pass) or die("Can not connect." . mysql_er开发者_如何学Goror());
mysql_select_db($db) or die("Can not connect.");

$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";

$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= '"'.$rowr[$j].'",'; 
}
$csv_output .= "\n";
}

$filename = $file."_".date("Y-m-d",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
exit;
?>

any ideas?


$query = mysql_query('TRUNCATE TABLE '.$table);

or if you want to archive them

$query = mysql_query('RENAME TABLE db_name.'.$table.' TO db_name.'.$table.time());
$query = mysql_query('CREATE TABLE db_name.'.$table.' ( /* table structure */ )');


i'm extremely confused as to why the data needs to be 'fresh' and why you have a database table that can be cleared out at will.

can't you just export whatever data you want directly to a CSV and skip the whole writing-to-and-subsequently-deleting-the-db step?

I would think that whatever data you're collecting and storing in this 'temporary table' can be accumulated elsewhere, and then when they want the CSV, you can just get all the data you need at that time instead of keeping around a pointless table.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜