CSV fwrite needs headers

Hello all.

Need some help. How do I get the headers to be included in the csv this creates:

$data = array();

   $result = mysql_query("SELECT listid,email FROM test");

   while($r=mysql_fetch_assoc($result))
   {    
       $data[]=$r;
   }
         
   $filename = "D:\\sites\\email\\test\\test.csv";
     
   $file = fopen( $filename, "w" );
   foreach($data as $row)
   {
        fwrite($file, $row['listid']); //we expect column_a to be numeric
        fwrite($file, ','); //comma
        fwrite($file, '"'.$row['email'].'"'); //we expect column_c to be string
        fwrite($file, "\r\n");//end of line;
   }
   fclose( $file );

What is the proper fwrite command for the headers to be included?
jsteidlAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Michael701Commented:
well since you know the field names you could do a simple

        fwrite($file, "'listid','email'\r\n");

of look at mysql_field_name and mysql_num_fields

ask if you need code for this
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jsteidlAuthor Commented:
I hate it when its staring me in the face. Well it's Monday so I guess that is ok.

Thanks,

Jake
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.