我的 pdf 转换显示什么都没有..但我在 mysql.. (fpdf) 中有数据
my pdf convert show nothing.. but i have datas in mysql.. (fpdf)
我的代码在这里 ;
require('pdf_creat/fpdf.php');
include ('class/db.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
$sql = "select * from students";
$result = dbConnect()->prepare($sql);
$row=$result->fetchAll(PDO::FETCH_ASSOC);
while($rows=array_shift($row)) {
$studid = $rows[0];
$name = $rows[1];
$address = $rows[2];
$class = $rows[3];
$phone = $rows[4];
$email = $rows[5];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
我只做了系统,但是当我显示时,它什么也没显示。我想在 mysql 中显示数据。我的代码有什么错误吗?我搜索了很多答案,但他们都不让我满意。如果我有任何错误或者我必须在我的代码之间添加任何功能,请任何人告诉我?
因为,我不知道 include('class/db.php');包含。我使用 PDO 连接来测试你的情况:
require('fpdf.php');
function getConnection() {
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='testdb';
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
$sql = "select * from students";
$db = getConnection();
$stmt = $db->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
foreach($rows as $key=>$value){
$studid = $value['studid'];
$name = $value['name'];
$address = $value['address'];
$class = $value['class'];
$phone = $value['phone'];
$email = $value['email'];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
唯一的主要变化是 i 在 pdo 关联数组上循环的方式。我正在访问从数据库读取的每一行的键,所以而不是
$studid = $rows[0];
我正在访问说出 id
$studid = $rows['id'];
我的代码在这里 ;
require('pdf_creat/fpdf.php');
include ('class/db.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
$sql = "select * from students";
$result = dbConnect()->prepare($sql);
$row=$result->fetchAll(PDO::FETCH_ASSOC);
while($rows=array_shift($row)) {
$studid = $rows[0];
$name = $rows[1];
$address = $rows[2];
$class = $rows[3];
$phone = $rows[4];
$email = $rows[5];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
我只做了系统,但是当我显示时,它什么也没显示。我想在 mysql 中显示数据。我的代码有什么错误吗?我搜索了很多答案,但他们都不让我满意。如果我有任何错误或者我必须在我的代码之间添加任何功能,请任何人告诉我?
因为,我不知道 include('class/db.php');包含。我使用 PDO 连接来测试你的情况:
require('fpdf.php');
function getConnection() {
$dbhost='localhost';
$dbuser='root';
$dbpass='';
$dbname='testdb';
$dbh = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $dbh;
}
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();
$sql = "select * from students";
$db = getConnection();
$stmt = $db->query($sql);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$db = null;
foreach($rows as $key=>$value){
$studid = $value['studid'];
$name = $value['name'];
$address = $value['address'];
$class = $value['class'];
$phone = $value['phone'];
$email = $value['email'];
$pdf->Cell(25,7,$studid);
$pdf->Cell(30,7,$name);
$pdf->Cell(40,7,$address);
$pdf->Cell(30,7,$class);
$pdf->Cell(30,7,$phone);
$pdf->Cell(30,7,$email);
$pdf->Ln();
}
$pdf->Output();
唯一的主要变化是 i 在 pdo 关联数组上循环的方式。我正在访问从数据库读取的每一行的键,所以而不是
$studid = $rows[0];
我正在访问说出 id
$studid = $rows['id'];