- Someone
- Friday, August 10th, 2007 at 3:11:46pm MDT
- <?
- // Connect database
- $database="tutorial";
- $table="name_list";
- mysql_connect("localhost","","");
- mysql_select_db("tutorial");
- $result=mysql_query("select * from $table");
- $out = '';
- // Get all fields names in table "name_list" in database "tutorial".
- $fields = mysql_list_fields(tutorial,$table);
- // Count the table fields and put the value into $columns.
- $columns = mysql_num_fields($fields);
- // Put the name of all fields to $out.
- for ($i = 0; $i < $columns; $i++) {
- $l=mysql_field_name($fields, $i);
- $out .= '"'.$l.'",';
- }
- $out .="\n";
- // Add all values in the table to $out.
- while ($l = mysql_fetch_array($result)) {
- for ($i = 0; $i < $columns; $i++) {
- $out .='"'.$l["$i"].'",';
- }
- $out .="\n";
- }
- // Open file export.csv.
- $f = fopen ('export.csv','w');
- // Put all values from $out to export.csv.
- fputs($f, $out);
- fclose($f);
- header('Content-type: application/csv');
- header('Content-Disposition: attachment; filename="export.csv"');
- readfile('export.csv');
- ?>
advertising
Update the Post
Either update this post and resubmit it with changes, or make a new post.
You may also comment on this post.
Please note that information posted here will expire by default in one month. If you do not want it to expire, please set the expiry time above. If it is set to expire, web search engines will not be allowed to index it prior to it expiring. Items that are not marked to expire will be indexable by search engines. Be careful with your passwords. All illegal activities will be reported and any information will be handed over to the authorities, so be good.