How would I export one record (from mySql) to a csv file

Hello, using php, does anyone know how I would export the current record into a csv file?

thanks for any help.


You select the record you want (SELECT * FROM table WHERE id = X), and the echo the outcome in a CSV format (separating the values with commas), and in the header you force this file to be a CSV (downloadable) file:

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\\"yourfile.csv\\"");

Paulsp, Thank you very much! I got it working thanks to you…

I have another question regarding this…

The csv file shows the data, but does not include the field names (last Name, First Name, Address, etc)… I was wondering if there is a way to have it also download the field names into the csv file…

  • as a manual work around, I did just manuallly insert them into the variable myself,
$columnNames = "First Name, Last Name, Status" . "\

but would rather they automatically be inserted into the csv file with the data.

Ive been at it for awhile, but can not get it. fyi: they are displayed on the page before pressing the submit button (to download as csv).

Is there a way to force the downloaded file to use semi colons as the delimiter instead of comma?

some of my fields have commas in them, so rather change the delimiter if its possible.