Allow users to pull temporary data then delete table data (headers remain)?
Posted
by JM4
on Stack Overflow
See other posts from Stack Overflow
or by JM4
Published on 2010-05-27T20:21:14Z
Indexed on
2010/05/27
20:51 UTC
Read the original article
Hit count: 112
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_error());
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?
© Stack Overflow or respective owner