Quick Search:
 
 PHP Code: Export mySQL results to CSV Jump to:  
Category: >> PHP Code >> Export mySQL results to CSV  

<< lastnext >>

Snippet Name: Export mySQL results to CSV

Description: This script will export the results of a mySQL query to a Comma Separated Value (CSV ) format. Very handy for importing and exporting data.

Also see:
» Export mySQL results to CSV
» Get Query String with Javascript
» UPDATE: Update based on a query
» EXPORT
» Display stock quotes from a CSV file
» A minus B query
» Extract Search Query from Referer
» Export table to Excel or MS Word file
» Export table to pipe delimited file
» Update and Subtract single query
» CaSe sEnsiTvE query

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 16th, 2009

// Export to CSV
IF($_GET['action'] == 'export') {
 
$rsSearchResults = mysql_query($sql, $db) or DIE(mysql_error());
 
$out = '';
 
$fields = mysql_list_fields('database','table',$db);
 
$columns = mysql_num_fields($fields);
 
 
// Put the name of all fields
 
FOR ($i = 0; $i < $columns; $i++) {
 
$l=mysql_field_name($fields, $i);
 
$out .= '"'.$l.'",';
 
}
 
$out .="\n";
 
 
// Add all values in the table
 
WHILE ($l = mysql_fetch_array($rsSearchResults)) {
 
FOR ($i = 0; $i < $columns; $i++) {
 
$out .='"'.$l["$i"].'",';
 
}
 
$out .="\n";
 
}
 
// Output to browser with appropriate mime type, you choose ;)
 
HEADER("Content-type: text/x-csv");
 
//header("Content-type: text/csv");
 
//header("Content-type: application/csv");
 
HEADER("Content-Disposition: attachment; filename=search_results.csv");
 
ECHO $out;
 
EXIT;
 
}
 


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org