You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FPDF中While循环仅输出单条数据的技术求助

问题描述

希望基于$bulan字段从数据库调用数据,数据库中该字段存在多条相同数据,但调用时仅显示第一条记录。已使用While循环但无效,附上代码请求协助解决:

<?php
require_once("fpdf/fpdf.php");
require_once('koneksi.php');

$no = 1;
$bulan = $_POST['bulan'];

$data = mysqli_query($conn, "SELECT proses1.*, customer.nickname FROM proses1 inner join
customer on customer.id_cust = proses1.id_cust WHERE proses1.tgl = '$bulan'");
while ($row = mysqli_fetch_array($data)) {
$pdf = new FPDF('l','mm','A4');
// membuat halaman baru
$pdf -> AddPage();
$pdf -> SetFont('Times','B','10');
// judul
$pdf->Cell(15,10,'',0,0,'L');
$pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');

$pdf -> SetFont('Times','','12');
$pdf->Cell(260,10,'Rekap Proses 1',0,2,'C');
$pdf -> image('dist/img/gandingrbg.png',10,13,15,15 );

$pdf -> SetFont('Times','','10');
$pdf->Cell(230,10,'Periode Bulan :',0,0,'R');
$pdf->Cell(20,10,date('d-M-Y', strtotime($_POST['bulan'])),0,1,'R');

$pdf -> SetFont('Times','B','10');
$pdf->Cell(10,10,'No',1,0,'C');
$pdf->Cell(17,10,'CUST',1,0,'C');
$pdf->Cell(50,10,'Nama Part',1,0,'C');
$pdf->Cell(17,10,'Kode Part',1,0,'C');
$pdf->Cell(30,10,'Nama Proses',1,0,'C');
$pdf->Cell(40,10,'Quantity Masuk',1,0,'C');
$pdf->Cell(45,10,'Quantity Not Good',1,0,'C');
$pdf->Cell(30,10,'Keterangan',1,0,'C');
$pdf->Cell(40,10,'Tanggal Dikerjakan',1,1,'C');

$pdf -> SetFont('Times','','10');
$pdf->Cell(10,10,$no++,1,0,'C');
$pdf->Cell(17,10,$row['nickname'],1,0,'C');
$pdf->Cell(50,10,$row['nama_part'],1,0,'C');
$pdf->Cell(17,10,$row['kode_part'],1,0,'C');
$pdf->Cell(30,10,$row['nama_proses'],1,0,'C');
$pdf->Cell(40,10,$row['qty_aktual'],1,0,'C');
$pdf->Cell(45,10,$row['qty_ng'],1,0,'C');
$pdf->Cell(30,10,$row['keterangan'],1,0,'C');
$pdf->Cell(40,10,$row['tgl'],1,1,'C');

$pdf->Output();
}
?>

问题分析

核心错误是PDF初始化、页面创建、标题表头以及输出操作都被放在了While循环内部:

  1. 每次循环都会新建一个FPDF实例,覆盖之前的内容,导致每条数据都生成独立的PDF文件,浏览器只会接收第一个输出的PDF。
  2. $pdf->Output()在循环内执行,第一次循环就会输出PDF并终止脚本,后续循环完全不会运行,因此只显示第一条数据。

修正后的代码
<?php
require_once("fpdf/fpdf.php");
require_once('koneksi.php');

$no = 1;
$bulan = $_POST['bulan'];

// 使用预处理语句防止SQL注入
$stmt = $conn->prepare("SELECT proses1.*, customer.nickname FROM proses1 INNER JOIN customer ON customer.id_cust = proses1.id_cust WHERE proses1.tgl = ?");
$stmt->bind_param("s", $bulan);
$stmt->execute();
$data = $stmt->get_result();

// PDF初始化、页面添加、标题表头仅执行一次
$pdf = new FPDF('l','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Times','B','10');
// 标题
$pdf->Cell(15,10,'',0,0,'L');
$pdf->Cell(100,20,'PT Ganding Toolsindo',0,2,'L');

$pdf->SetFont('Times','','12');
$pdf->Cell(260,10,'Rekap Proses 1',0,2,'C');
$pdf->image('dist/img/gandingrbg.png',10,13,15,15 );

$pdf->SetFont('Times','','10');
$pdf->Cell(230,10,'Periode Bulan :',0,0,'R');
$pdf->Cell(20,10,date('d-M-Y', strtotime($bulan)),0,1,'R');

$pdf->SetFont('Times','B','10');
$pdf->Cell(10,10,'No',1,0,'C');
$pdf->Cell(17,10,'CUST',1,0,'C');
$pdf->Cell(50,10,'Nama Part',1,0,'C');
$pdf->Cell(17,10,'Kode Part',1,0,'C');
$pdf->Cell(30,10,'Nama Proses',1,0,'C');
$pdf->Cell(40,10,'Quantity Masuk',1,0,'C');
$pdf->Cell(45,10,'Quantity Not Good',1,0,'C');
$pdf->Cell(30,10,'Keterangan',1,0,'C');
$pdf->Cell(40,10,'Tanggal Dikerjakan',1,1,'C');

$pdf->SetFont('Times','','10');
// 循环仅处理数据行
while ($row = mysqli_fetch_array($data)) {
    $pdf->Cell(10,10,$no++,1,0,'C');
    $pdf->Cell(17,10,$row['nickname'],1,0,'C');
    $pdf->Cell(50,10,$row['nama_part'],1,0,'C');
    $pdf->Cell(17,10,$row['kode_part'],1,0,'C');
    $pdf->Cell(30,10,$row['nama_proses'],1,0,'C');
    $pdf->Cell(40,10,$row['qty_aktual'],1,0,'C');
    $pdf->Cell(45,10,$row['qty_ng'],1,0,'C');
    $pdf->Cell(30,10,$row['keterangan'],1,0,'C');
    $pdf->Cell(40,10,$row['tgl'],1,1,'C');
}

// 所有数据处理完成后统一输出PDF
$pdf->Output();
?>

修正说明
  • 将PDF初始化、页面创建、标题表头代码移到循环外,确保所有数据都添加到同一个PDF文件中。
  • 把$pdf->Output()移到循环结束后,保证所有数据处理完成再输出完整PDF。
  • 替换为预处理语句,避免SQL注入风险,提升代码安全性。

内容的提问来源于stack exchange,提问作者Almas Dhiya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 11:50:21