Guys

Is there a way to export the record of a table in csv format and then send that csv as attachment in mail to anyone using php?

Thanks

Here is a code for exporting table to csv.

<?
	mysql_connect('localhost','root','');
	mysql_select_db('test');
	
	$sql = "select student_name,student_sirname from student";

	// Query Database	
	$filename = 'file.csv';		
	$rsSearchResults = mysql_query($sql) or die(mysql_error());
	
	$out = '';
	
	// fiels to export
	$out .='Student Name,Sirname';
	$out .="\n";
	
	// Add all values in the table
	while ($l = mysql_fetch_array($rsSearchResults)) {
	for ($i = 0; $i < 2; $i++) {
	$out .=''.$l["$i"].',';
	}
	$out .="\n";
	}
	// Output to browser with appropriate mime type
	header("Content-type: text/x-csv");	
	header("Content-Disposition: attachment; filename=$filename");
	echo $out;
	exit;	
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.