View Full Version : Export To CSV

08-20-2007, 05:27 PM
Yes another csv question, I have found many topics but not what I need, I only want to export 4 columns from my database, and also specify column headings for the column names... Is that possible?

08-20-2007, 08:16 PM
Have a look at fputcsv() (http://us2.php.net/manual/en/function.fputcsv.php)

08-21-2007, 04:30 PM
Thanks I have read through that page over and over it doesn't really help me though... If ANYONE has a clean way of doing the following would be great. I have read about 25 posts from Google searches every single one different every single one using different PHP elements...

What I want to do is pull out from my table into a csv file 5 columns, FirstName, LastName, Email, Mobile... I then want to add to the top of the .csv seperate headings... So basically I want to achieve this...

Fname, Sname, Email, Mobile
Tom, Smith, tom@smith.com, 0410000000
James, Smith, james@smith.com, 0410000000

I have tried every which way and can't get a result... I'm not sure where to go from this point... How do I write to the actual .csv file? I mean what do I need to add to the above code to get a file?

08-21-2007, 05:13 PM
You read that page over and over but never thought to try the example? It's about as "clean" as you're going to find. Here's the example:


$list = array (

$fp = fopen('file.csv', 'w');

foreach ($list as $line) {
fputcsv($fp, split(',', $line));


Once you see that work, you'll be able to replace the array called $list with your own array built from your database query. I'm not sure how much more straightforward it can get for you.