<?php
mysql_connect("localhost","mmaaaco_ksecor","hamilton");
mysql_select_db("mmaaaco_ksecor");
//will back up sp13members every Sunday night at midnight....try to get $25 each time, and invoice them monthly...  
$file_type = "vnd.ms-excel";
$file_ending = "xls";
 
$sql = "Select * from sp13members";

$Use_Title = 1;
$now_date = date('m-d-Y H:i');
$title = "Dump For Table $table from Database $now_date";

//execute query
$result = mysql_query($sql);

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";
	$content  .= trim($schema_insert);
	$content .= "\n";
	}
}

 $to="kdsecor@gmail.com";
   $subject="Your db backup on ".date('m d, Y');

   // generate a random string to be used as the boundary marker
   $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x";
   $headers = "MIME-Version: 1.0\r\n" .
      "Content-Type: multipart/mixed;\r\n" .
      " boundary=\"{$mime_boundary}\"";
   $message = "This is a multi-part message in MIME format.\n\n" .
      "--{$mime_boundary}\n" .
      "Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
      "Content-Transfer-Encoding: 7bit\n\n" .
   $message . "\n\n";
   $name = 'database'.$now_date.'.xls';
   $type = 'application/vnd.ms-excel';
   $data = chunk_split(base64_encode($content));
   $message .= "--{$mime_boundary}\n" .
            "Content-Type: {$type};\n" .
            " name=\"{$name}\"\n" .
     
            "Content-Transfer-Encoding: base64\n\n" .
         $data . "\n\n";
 $message.="--{$mime_boundary}--\n";
   // now we just send the message
  mail($to, $subject, $message, $headers);

 ?>