my codes are here ;
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();
I did simply the system but when i display, it show nothing. I want to display datas in mysql.. is there any error in my codes? I searched many answers but they did not satisfied me.. please anyone can say if I have any error or must I add any function between my codes ?
Because, I don't know what include('class/db.php'); contains. I used PDO connection to test your case:
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();
The only major change is the way the i loop over the pdo associative array. I am accessing the keys of each of the rows read from the db, so instead of
$studid = $rows[0];
I am accessing say the id
$studid = $rows['id'];