Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Export mySQL results to CSV Jump to:  
Category: >> PHP Code >> Export mySQL results to CSV Bookmark and Share

<< 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
» 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;

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 32 users online    © 2009 psoug.org

Forgot your password?