Link to home
Start Free TrialLog in
Avatar of pambrose1
pambrose1

asked on

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;
}
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

"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.
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

Avatar of pambrose1
pambrose1

ASKER

Thanks - my only comment - does this work locally?
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
ASKER CERTIFIED SOLUTION
Avatar of pambrose1
pambrose1

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
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.