Reputation: 1
When I try to export mysql data to pdf using php I get below errors;
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\SwaziTour\rpt_payment_history.php on line 48
Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\SwaziTour\rpt_payment_history.php on line 52
Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file (output started at C:\xampp\htdocs\SwaziTour\rpt_payment_history.php:48)' in C:\xampp\htdocs\SwaziTour\fpdf.php:271 Stack trace: #0 C:\xampp\htdocs\SwaziTour\fpdf.php(1052): FPDF->Error('Some data has a...') #1 C:\xampp\htdocs\SwaziTour\fpdf.php(999): FPDF->_checkoutput() #2 C:\xampp\htdocs\SwaziTour\rpt_payment_history.php(57): FPDF->Output() #3 {main} thrown in C:\xampp\htdocs\SwaziTour\fpdf.php on line 271
My Code is as below;
<?php
$result = mysql_query("select p.payment_id, u.company, p.description, p.payment_date, p.amount, p.status
FROM payment p
left join user u on p.user_id = u.user_id
order by p.payment_id desc")or die(mysql_error());
$header = mysql_query("SELECT UCASE(`COLUMN_NAME`)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='crud'
AND `TABLE_NAME`='payment'
and `COLUMN_NAME` in ('payment' p,'user u')");
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(95,12,$column_heading,1);
}
foreach($result as $row) {
$pdf->Ln();
foreach($row as $column)
$pdf->Cell(95,12,$column,1);
}
$pdf->Output();
?>
Upvotes: 0
Views: 2960
Reputation: 21513
you don't actually fetch the data after running the query. read up on http://php.net/manual/en/function.mysql-fetch-array.php , and maybe try something like
$res = mysql_query("SELECT UCASE(`COLUMN_NAME`)
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='crud'
AND `TABLE_NAME`='payment'
and `COLUMN_NAME` in ('payment' p,'user u')");
while(false!==($heading=mysql_fetch_array($res,MYSQL_ASSOC))){
foreach($heading as $column_heading)
$pdf->Cell(95,12,$column_heading,1);
}
}
Upvotes: 0