[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 672
  • Last Modified:

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?
0
jsteidl
Asked:
jsteidl
1 Solution
 
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
 
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

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now