<?php
/*mysql_connect("localhost","mmaaaco_mesa","987demo123");
mysql_select_db("mmaaaco_mesa");*/

$submit = $_POST['submit'];
if ($submit != "")
    {
$table = $_POST['excel'];
$sql = "Select * from $table";

$Use_Title = 1;
$now_date = date('m-d-Y H:i');
$title = "Dump For Table $table from Database $now_date";
$w = $_POST['w'];
//execute query
$result = mysql_query($sql);
if (isset($w) && ($w==1))
{
	$file_type = "msword";
	$file_ending = "doc";
}else {
	$file_type = "vnd.ms-excel";
	$file_ending = "xls";
}
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=database_dump.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");


if (isset($w) && ($w==1)) 
{
	if ($Use_Title == 1)
	{
		echo("$title\n\n");
	}
	$sep = "\n"; 

	while($row = mysql_fetch_row($result))
	{
		$schema_insert = "";
		for($j=0; $j<mysql_num_fields($result);$j++)
		{
		$field_name = mysql_field_name($result,$j);
		$schema_insert .= "$field_name:\t";
			if(!isset($row[$j])) {
				$schema_insert .= "NULL".$sep;
				}
			elseif ($row[$j] != "") {
				$schema_insert .= "$row[$j]".$sep;
				}
			else {
				$schema_insert .= "".$sep;
				}
		}
		$schema_insert = str_replace($sep."$", "", $schema_insert);
		$schema_insert .= "\t";
		print(trim($schema_insert));
		print "\n----------------------------------------------------\n";
	}
}else{
	if ($Use_Title == 1)
	{
		echo("$title\n");
	}
	$sep = "\t";
	for ($i = 0; $i < mysql_num_fields($result); $i++)
	{
		echo mysql_field_name($result,$i) . "\t";
	}
	print("\n");

	while($row = mysql_fetch_row($result))
	{
		$schema_insert = "";
		for($j=0; $j<mysql_num_fields($result);$j++)
		{
			if(!isset($row[$j]))
				$schema_insert .= "NULL".$sep;
			elseif ($row[$j] != "")
				$schema_insert .= "$row[$j]".$sep;
			else
				$schema_insert .= "".$sep;
		}
		$schema_insert = str_replace($sep."$", "", $schema_insert);

		$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
		$schema_insert .= "\t";
		print(trim($schema_insert));
		print "\n";
	}
}

     }
else
    {

$sql = mysql_query("show tables");

?>

<form action = "excel2.php" method = "post">
<select name = "excel">
<?
while ($row = mysql_fetch_array($sql))
   {
echo '<option value = "'.$row[0].'">'.$row[0].'</option>';
   }
?>
In which format?
<select name = "w">
<option value = "1">Word</option>
<option value = "2">Excel</option>
</select><br/>
<input type = "submit" name = "submit" value = "Download Spreadsheet">
</form>
<?
}
?>