Export MySql table data to CSV

<?php
$dbServer = 'localhost'; 	
$dbUser = 'root';       	
$dbPass  = '';        
$dbName='db';  
$table = 'test';

$dbl =@mysql_connect($dbServer,$dbUser,$dbPass); mysql_select_db($dbName,$dbl); mysql_query('SET NAMES utf8');

$result = mysql_query('show columns from '.$table.''); $num = mysql_num_rows ($result); $csv_out = ''; $values = mysql_query('select * from '.$table.''); while ($row = mysql_fetch_row($values)) { for ($j=0;$j<$num;$j++) { $csv_out .= '''.$row[$j].'''; if($j < ($num-1)){$csv_out .= ';';} else{$csv_out .= '';} } $csv_out .= 'n'; }

$filename = $table.'_'.date('Y-m-d_H-i-s',time()); header('Content-disposition: filename='.$filename.'.csv'); print $csv_out; exit; ?>