Creating a CSV file without every column heading

Bruce Gust
Bruce Gust used Ask the Experts™
on
This is the code that I'm using to create a csv file based on a query:

$fp = fopen('spreadsheets/voter_not_validated.csv', 'w');
// we capture the structure in order to catch the field names
$headings=array();
$bruce_query = "DESCRIBE registration";
$bruce_recordset = mysqli_query($cxn, $bruce_query)
or die ("Couldn't excute query.");
while($bruce_row=mysqli_fetch_assoc($bruce_recordset)) {
    $headings[ ]=$bruce_row['Field'];
}
fputcsv($fp, $headings, ',', '"'); // this save the headings whether you have data  or not.

$bruce_query = "select * from registration WHERE registration_type <> 'fan' AND (voter_validation='' OR voter_validation=' ') AND
songwriting_contest BETWEEN '2011-03-01' AND '2011-12-31' order by first_name";
$bruce_recordset = mysqli_query($cxn, $bruce_query)
or die ("Couldn't excute query.");
while($bruce_row=mysqli_fetch_assoc($bruce_recordset)) {
    fputcsv($fp, $bruce_row, ',', '"');
}
fclose($fp);
echo "voter_not_validated.csv is created<BR>";

The great news is that it works wonderfully, however...

I want to create a csv file that only includes those columns that are relevant to the query. How do I initiate my csv file in a way where I'm not having to include all the columns in the original database? Right now, the "describe" dynamic, while it's working, is overkill and I'm having to doctor the resulting file a little bit before I can use it the way that I need to.

How do you create the csv file so it only has those columns that are referenced in the query as opposed to every column in the table sitting in the database?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2011
Top Expert 2016
Commented:
Instead of SELECT * you can make the SELECT with just the names of the columns you want to retrieve.
What Ray posted will indeed select only the columns that you want. Here's how to use that method to write those columns out in the header regardless of whether you have any data in your result set.
<?php
$fp = fopen('spreadsheets/voter_not_validated.csv', 'w');
$headings = array(
    # List the names of the fields here
    # Be sure to quote any field names that contain spaces using backticks, i.e. `some column`
    'column_1',
    'column_2',
    'column_3',
    'column_4',
    # ...
);
fputcsv($fp, $headings, ',', '"');

$bruce_query = sprintf("select %s from registration WHERE registration_type <> 'fan' AND (voter_validation='' OR voter_validation=' ') AND
songwriting_contest BETWEEN '2011-03-01' AND '2011-12-31' order by first_name", implode(',', $headings));
$bruce_recordset = mysqli_query($cxn, $bruce_query)
or die ("Couldn't excute query.");
while($bruce_row=mysqli_fetch_assoc($bruce_recordset)) {
    fputcsv($fp, $bruce_row, ',', '"');
}
fclose($fp);
echo "voter_not_validated.csv is created<BR>";
?>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial