Return to Snippet

Revision: 50794
at September 3, 2011 11:34 by FatFolderDesigner


Initial Code
require '../includes/init.php'; // This connects to the database
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=client_export.csv");
header("Pragma: no-cache");
header("Expires: 0");
$q=mysql_query('select * from users');
while($r=mysql_fetch_array($q)){
	foreach($r as $k => $v){
		if(is_numeric($k)){
			echo '"'.str_replace('"', '""', $v).'",';
		}
	}
	echo "\n";
}

Initial URL
http://fatfolderdesign.com/376/php/simple-mysql-to-csv

Initial Description
Just a short little script that takes a mysql query and turns it into a csv file that downloads automatically. If your interested in the details you can hit up the link.

Initial Title
Simple MySQL to CSV

Initial Tags
mysql, php, simple, csv

Initial Language
PHP