当我尝试使用php将mysql数据导出为pdf时,出现以下错误;
警告:第48行的C:\ xampp \ htdocs \ SwaziTour \ rpt_payment_history.php中为foreach()提供的参数无效
警告:第52行的C:\ xampp \ htdocs \ SwaziTour \ rpt_payment_history.php中为foreach()提供的参数无效
致命错误:未捕获的异常'Exception'和消息'FPDF错误:已经输出了一些数据,无法发送PDF文件(输出从C:\ xampp \ htdocs \ SwaziTour \ rpt_payment_history.php:48开始)在C:\ xampp \ htdocs \ SwaziTour \ fpdf.php:271中堆栈跟踪:#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}在第271行的C:\ xampp \ htdocs \ SwaziTour \ fpdf.php中抛出
我的代码如下;
<?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();
?>
答案 0 :(得分:0)
运行查询后,您实际上并没有获取数据。阅读http://php.net/manual/en/function.mysql-fetch-array.php,然后尝试尝试类似的
$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);
}
}