0

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

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vibhaJ
1

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;	
?>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.