• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

PHP Export to local CSV file

Trying to simplify an export.  I have MySQL on a remote server and need to do a mailmerge so need the CSV on the local machine.  

Also - if anyone could show me how to limit the export to a single record, that'd be nice too!

I can export a table to CSV locally, but it always launches Excel - I just want to produce a CSV that sits in a particular location and doesn't launch Excel ...

Code pasted here;

$table = $tablename;
$file = $exportfile;
 
$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_H-i",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;
}
0
pambrose1
Asked:
pambrose1
  • 4
  • 3
1 Solution
 
Ray PaseurCommented:
"but it always launches Excel"

That is a function of the file associations on your local machine.  If you produce the CSV data but name the file something.txt you can open it with notepad instead of Excel.
0
 
Ray PaseurCommented:
If you want to save the file on the server, substitute this...

$filename = $file."_".date("Y-m-d_H-i",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;

with something like the code snippet.  Note that I have not tested it, but the principle is valid.  You might want to echo out a link to the newly created file of CSV data.
$filename = $file . "_" . date("Y-m-d_H-i");
// SEE http://us.php.net/manual/en/function.file-put-contents.php
file_put_contents($filename, $csv_output);

Open in new window

0
 
pambrose1Author Commented:
Thanks - my only comment - does this work locally?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Ray PaseurCommented:
Sorry, locally for me is not the same as locally for you.  Please try it and post back so we both know the answer.  If you find it doesn't do what you want, please show us as much information as you can about what it did that you did not like.  Thanks, ~Ray
0
 
pambrose1Author Commented:
Hi Ray - I've actually changed the script now so that I FTP the file from the server to client - probably a better way anyway!

Thanks for your help

Peter
0
 
Ray PaseurCommented:
Your original question was, "I can export a table to CSV locally, but it always launches Excel - I just want to produce a CSV that sits in a particular location and doesn't launch Excel ..."

What was wrong with my answer?
0
 
pambrose1Author Commented:
I was looking for the wrong solution - the idea was that the file should be stored locally, and the only way I'd found to do that resulted in Excel being launched.  The solution offered meant that the file would be stored on the server, (i.e. not locally) which didn't solve the problem.

Either way, the issue to store a file locally on a PC which does not automatically get opened by Excel was not resolved by this answer.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now