Export MySQL to Excel Using PHP

The code below will export every column name and value from your database into an excel document.

Note: Delete the line spaces if you receive a parse error.

" . mysql_error() . "
" . mysql_errno());
//select database
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database:
" . mysql_error(). "
" . mysql_errno());
//execute query
$result = @mysql_query($sql,$Connect)
or die("Couldn't execute query:
" . mysql_error(). "
" . mysql_errno());
$file_ending = "xls";

//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");

/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "t"; //tabbed character

//start of printing column names as names of MySQL fields
for ($i = 0; $i < mysql_num_fields($result); $i++) { echo mysql_field_name($result,$i) . "t"; } print("n"); //end of printing column names //start while loop to get data while($row = mysql_fetch_row($result)) { $schema_insert = ""; for($j=0; $j