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;
}
pambrose1Asked:
Who is Participating?
 
pambrose1Connect With a Mentor Author 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:
"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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
pambrose1Author Commented:
Thanks - my only comment - does this work locally?
0
 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.