FPDF现在失败了-已经成功了


FPDF fail now - Has worked?

我有FPDF从Mysql数据库生成一个成员列表到PDF列表。但是在我的网站主机更新了php版本后,我的成员列表没有工作!

现在显示:

警告:为foreach()提供的参数无效/客户/8/f/b/xxx.com/httpd.www/test/printmedlemsliste.php在线FPDF错误:一些数据已经输出,无法发送PDF文件(输出开始于/客户/8/f/b/xxx.com/httpd.www/test/printmedlemsliste.php: 46)

我的代码:
<?php
require('fpdf.php');
class People {
    public function all() {
        try {
            $db = new PDO('mysql:host=localhost;dbname=xxx_com;charset=UTF-8', 'xxx_com', 'xxx');
            $query = $db->prepare("SELECT o.user_post, o.user_name1, o.user_name2, o.user_address1, o.user_address2, o.user_phone1, o.user_phone2, c.user_email, o.user_type FROM e107_user c, e107_user_extended o WHERE c.user_id = o.user_extended_id Order By user_name1");
            $query->execute();
            $people = $query->fetchAll(PDO::FETCH_ASSOC);
        } catch (PDOException $e) {
            //echo "Exeption: " .$e->getMessage();
            $result = false;
        }
        $query = null;
        $db = null;
        return $people;
    }
}

class PeoplePDF extends FPDF{
// Create basic table
    public function BasicTable($header, $data)
    {
        // Header
        $this->SetFont('','B', 12);
        $this->Cell(0,6,'Medlemsliste'.'',1,1,'C');
        $this->SetFont('','B', 9);
        $dateinvoice = substr( $invoice[ 'dateinvoice' ], 6 ).substr( $invoice[ 'dateinvoice' ], 3, 2 ).substr( $invoice[ 'dateinvoice' ], 0, 2 );
        $dateinvoice = date_create( $dateinvoice );
        $dateinvoice = false === $dateinvoice ? '' : $dateinvoice->format( 'd-M-Y' );
        $this->Cell( 10 ); $this->Cell( 17, 5, 'Print Dato:' ); $this->Cell( 17, 5, $dateinvoice, 0, 1 );
        $this->SetFillColor(255);
        $this->SetTextColor(0);
        $this->SetFont('','B');
        foreach ($header as $col) {
            //Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
        $this->Cell($col[1], 5, $col[0], 1, 0, 'L', true); }
        $this->Ln();
        // Data
               $this->SetFillColor(255);
        $this->SetTextColor(0);
        $this->SetFont('');
        foreach ($data as $row)
        {
            $i = 0;
            foreach ($row as $field) {
                $this->Cell($header[$i][1], 6, $field, 1, 0, 'L', true);
                $i++;
            }
            $this->Ln();
        }
    }
}

// Column headings
$header = array(
             array('Post',  20),
             array('Fornavn',  18),
             array('Efternavn', 46),
             array('Adresse',   40),
             array('Postnummer & By',         40),
             array('Telefon',       22),
             array('Mobil',       22),
             array('E-Mail',       57),
             array('Type',       12)
          );
// Get data

$people = new People();
$data = $people->all();
$pdf = new PeoplePDF();
$pdf->SetFont('Arial', '', 9);
$pdf->AddPage('L');
$pdf->Ln(0);
$pdf->SetTitle("Medlemsliste", boolean);
$pdf->SetAuthor("-");
$pdf->BasicTable($header,$data);
$pdf->Output('yourfilename.pdf','D');
?>

第46行是foreach ($data as $row)

如果我把//放在foreach ($data as $row)的前面,在第49行会出现一个新的错误。第49行是foreach ($row as $field) {

同样,如果我在第49行前面设置//,它会生成PDF文件,但在!

中没有数据。

有谁能帮忙吗?

这是因为$data不包含任何内容,所以循环失败。当您使用$data = $people->all();

执行查询时,确认您在$data中获得了正确的数据

试试var_dump($data);,看看你在all()函数中的return实际上返回一个结果。

我也会把你的try/catch改成这个

try {
        $db = new PDO('mysql:host=localhost;dbname=xxx_com;charset=UTF-8', 'xxx_com', 'xxx');
        $query = $db->prepare("SELECT o.user_post, o.user_name1, o.user_name2, o.user_address1, o.user_address2, o.user_phone1, o.user_phone2, c.user_email, o.user_type FROM e107_user c, e107_user_extended o WHERE c.user_id = o.user_extended_id Order By user_name1");
        $query->execute();
        $people = $query->fetchAll(PDO::FETCH_ASSOC);
        return $people;
    } catch (PDOException $e) {
        //echo "Exeption: " .$e->getMessage();
        return false;
    }