<?php
session_start();
set_time_limit(60*30);
//Get config
include('config.php');
//connect to the database
mysql_connect('localhost',$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$tbl = $_SESSION['session_Table'];
$fld_result = mysql_query("SHOW COLUMNS FROM ".$tbl);
if(isset($_SESSION['session_Criteria'])){
$arr = $_SESSION['session_Criteria'];
}
Include('getMySQL.php');
$myQuery .= " Limit 0, $maxOutputs";
$result = mysql_query($myQuery) or die (header('Location: index.php?view=SQL&msg=Error in query '.mysql_error()));
$num=mysql_numrows($result);
//set report name
$file = "Output/".date("Y_m_d_H_i_s").".csv";
//Get Field names
$fieldNames="";
$j = 0;
while ($j < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $j);
if (!$meta) {
echo "No column headings found<br />\n";
exit;
}
if($fieldNames == ""){
$fieldNames = "\"".$meta->name."\"";
}
else {
$fieldNames .= ",\"".$meta->name."\"";
}
$j++;
}
$fieldNames .= "\n";
//Open file and output header
$f = fopen($file, "w");
fwrite($f, $fieldNames);
//Now Get data
$i = 0;
while ($i < $num){
//Loop field names
$row = "";
$j = 0;
while ($j < mysql_num_fields($result)) {
$meta = mysql_fetch_field($result, $j);
$field = $meta->name;
$value = mysql_result($result,$i,$field);
if($row == ""){
$row = "\"".$value."\"";
}
else {
$row .= ",\"".$value."\"";
}
$j++;
}
$row = $row."\n";
fwrite($f, $row);
$i++;
}
fclose($f);
if(isset($zipExports)){
if($zipExports == "Y"){
Include('zipIt.php');
$newFile = zipIt($file);
if($newFile != $file){
$file = $newFile;
}
}
}
//Clean up Output folder
$path = 'Output/';
if ($handle = opendir($path)) {
while (false !== ($filex = readdir($handle))) {
if ((time()-filectime($path.$filex)) > 86400) {
@unlink($path.$filex);
}
}
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=".$file);
readfile($file);
?>