<?php
session_start();
require_once "main.php";
$eol = "\r\n";
$sql = 'Select * from datam where uemail = "'.$_SESSION['email'].'";';
$st = $db1->prepare($sql);
$st->execute();
$result = $st->fetchAll();
$c ='"First Name","Last Name","Address","City","State","Zipcode","Phone","Fax","Email","System Name",'.$eol;
for ($a=0;$a<count($result);$a++){
    $c = $c . '"'.$result[$a]['fname'].'","'.$result[$a]['lname'].'","'.$result[$a]['addr'].'","'.$result[$a]['city'].'","'.$result[$a]['state'].'","'.$result[$a]['zip'].'","'.$result[$a]['phone'].'","'.$result[$a]['fax'].'","'.$result[$a]['email'].'","'.$result[$a]['system'].'",'.$eol;
};
$file = "test.csv";
$txt = fopen($file, "wb") or die("Unable to open file!");
fwrite($txt, $c);
fclose($txt);

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);

?>