我有FPDF从Mysql数据库生成成员列表到PDF格式的列表。但是在我的网站主机更新了php版本之后,我的会员列表没有用了!
现在显示:
警告:为foreach()提供的参数无效 /customers/8/f/b/xxx.com/httpd.www/test/printmedlemsliste.php上线 46 FPDF错误:有些数据已经输出,无法发送PDF文件 (输出始于 /customers/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文件,但没有数据!
有人可以帮忙吗?
答案 0 :(得分:0)
那是因为$ data什么都没有,所以循环失败了。
使用$data = $people->all();
尝试var_dump($data);
看到你在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);
return $people;
} catch (PDOException $e) {
//echo "Exeption: " .$e->getMessage();
return false;
}