mysql make csv

I had to make a csv from the data coming from MySQL database, I came across this very easy method so I am sharing this to you people.

$query="SELECT * FROM tickets";
$Result = mysql_query($query) or die("Error: " . mysql_error());
header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=chargRep.csv");
while($row = mysql_fetch_row($Result))
{
print '"' . stripslashes(implode('","',$row)) . "\"\n";
}
exit;
?>
The verable $query may contain any mySQL query and above code will give you option to save or open the result returned from MySQL as a CSV file.
CSV files may be opened in MS Excel or as simple text files with fields separated by commas.

Posted in |