<?php include('database_connection.php');
if(isset($_GET['file']))
{
	$f=$_GET['file'];
$filename =$f.".xls";
// get field names 
$contents="".$filename."\n";
if(isset($_GET['query']))
{
	$query=$_GET['query'];
	$sql=$query;
	$fields="";
	$result=odbc_exec($connection,$sql);
	for($r=1;$r<=odbc_num_fields($result);$r++)
	{	
	$fields=odbc_field_name($result, $r);
	$contents .= $fields."\t";
	}
	$contents .="\n";
	if(odbc_num_rows($result)>0)
	{
		while($row=odbc_fetch_array($result))
		{
			foreach ($row as $key => $value) {
				# code...
	$contents .= utf8_encode($row[$key])."\t";
			}
	$contents .= "\n";
		
		}
	header('Content-type: application/ms-excel');
	header('Content-Disposition: attachment; filename='.$filename);
	echo $contents;
	}//end odbc_num_rows
}
}
 ?>
