I have a PHP code to export the data from mysql to excel sheet.
export_customer.php
<?php
include 'psl-config.php';
function export_excel_csv(){
$cust_name = $_GET['msg'];
$conn = mysql_connect(HOST, USER, PASSWORD);
$db = mysql_select_db(DATABASE,$conn);
$sql = "Select t1.custi_id_fk, t2.full_name, t2.phone_1, t2.phone_2, t1.amount from outwards_credit As t1 INNER JOIN cust_info As t2 ON t1.custi_id_fk = t2.cust_id where t1.bill_state = 0 and t1.custi_id_fk = ". $cust_name;
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
for($i = 0; $i < $num_fields; $i++ )
{
$header .= mysql_field_name($rec,$i)."\\t";
}
while($row = mysql_fetch_row($rec))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = "\\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\\t";
}
$line .= $value;
}
$data .= trim( $line ) . "\\n";
}
$data = str_replace("\\r" , "" , $data);
if ($data == "")
{
$data = "\\n No Record Found!\n";
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls");
header("Pragma: no-cache");
header("Expires: 0");
print "$header\\n$data";
}
if(isset($_GET['msg'])){
export_excel_csv();
} else {
echo "There are no data";
}
?>
I call this using a href from another file:
<a href= export_customer.php?msg=1> Export </a>
But I end up getting the following errors.
Warning: Cannot modify header information - headers already sent by (output started at /includes/export_customer.php:1) in /includes/export_customer.php on line 45
Warning: Cannot modify header information - headers already sent by (output started at /includes/export_customer.php:1) in /includes/export_customer.php on line 46
Warning: Cannot modify header information - headers already sent by (output started at /includes/export_customer.php:1) in /includes/export_customer.php on line 47
Warning: Cannot modify header information - headers already sent by (output started at /includes/export_customer.php:1) in /includes/export_customer.php on line 48 custi_id_fk\tfull_name\tphone_1\tphone_2\tamount\t\n"1"\t"customer"\t"123123"\t"987987"\t"100.00"\t\n"1"\t"customer"\t"123123"\t"987987"\t"10000.00"\t\n
Where am I going wrong???