I have do mysql query, i want to export the result data form mysql in term of csv files.

What is the code in php? Thanks!

You can make changes according to your database.

<?
	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.