警告:为第48行的c:\xampp\htdocs\swazitor\rpt\u payment\u history.php中的foreach()提供的参数无效

ogq8wdun  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(312)

当我尝试使用php将mysql数据导出为pdf时,我得到以下错误;
警告:为c:\xampp\htdocs\swazitour\rpt\u payment\u history.php中的foreach()提供的参数无效(第48行)
警告:为c:\xampp\htdocs\swazitor\rpt\u payment\u history.php中的foreach()提供的参数无效(第52行)
致命错误:未捕获异常“exception”,消息“fpdf error:某些数据已输出,无法发送pdf文件(输出从c:\xampp\htdocs\swazitor\rpt\u payment\u history开始)。php:48)'在c:\xampp\htdocs\swazitor\fpdf中。php:271 stack 跟踪:#0 c:\xampp\htdocs\swazitor\fpdf.php(1052):fpdf->error('某些数据有…。)#1 c:\xampp\htdocs\swazitor\fpdf.php(999):fpdf->\u checkoutput()#2c:\xampp\htdocs\swazitor\rpt\u payment\u history.php(57):fpdf->output()#3{main}在第271行的c:\xampp\htdocs\swazitor\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();
?>
shstlldc

shstlldc1#

在运行查询之后,实际上不会提取数据。仔细阅读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);
    }
}

或者更好,读一读http://wiki.hashphp.org/pdo_tutorial_for_mysql_developers -您正在使用的mysql_u1;api在php5.5中被弃用,在php7.0中被完全删除,如果您可以避免的话,今天就不应该使用它。

相关问题