0

I am trying to generate a PDF file from my table but my code is currently generating all the columns in my table to PDF i want to narrow it down to just 3 columns to be exported to PDF format

<?php
require_once("config.php");
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();

// code for print Heading of tables
$pdf->SetFont('Arial','B',8);   
$ret ="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS`, WHERE `TABLE_SCHEMA`='register' AND `TABLE_NAME`='new_record'";
$query1 = $dbh -> prepare($ret);
$query1->execute();
$header=$query1->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query1->rowCount() > 0)
{
foreach($header as $heading) {
foreach($heading as $column_heading)
$pdf->Cell(46,12,$column_heading,1);
}}
//code for print data
$sql = "SELECT * from  new_record ";
$query = $dbh -> prepare($sql);
$query->execute();
$results=$query->fetchAll(PDO::FETCH_OBJ);
$cnt=1;
if($query->rowCount() > 0)
{

foreach($results as $row) {
    $pdf->SetFont('Arial','',12);   
    $pdf->Ln();
    foreach($row as $column)
        $pdf->Cell(46,12,$column,1);
} }
$pdf->Output();
?>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Damola
  • 1
  • 1
  • 1
    Why use `INFORMATION_SCHEMA`? Just do `select columns, you, want from table`. – user3783243 Apr 08 '20 at 21:23
  • When your intention is to immediately iterate the result set rows (in the same "layer"), then `fetchAll()` is not recommended. The PHP manual will advice you to make iterated fetch calls. I don't think you told us which 3 column names you want to whitelist. – mickmackusa Apr 08 '20 at 21:24
  • I want to whitelist columns `id`,`sname`,`fname` – Damola Apr 08 '20 at 21:38
  • @mickmackusa i want to whitelist columns id, sname ,fname – Damola Apr 08 '20 at 21:45
  • @user3783243 done but still pull all the columns from the table – Damola Apr 08 '20 at 21:46
  • If you know exactly which columns that you want, then there is absolutely no need to try to fancy-up your script by making a dynamic call to the DB for the headers. You hardcode them, and also use the same columns in your next SELECT. – mickmackusa Apr 08 '20 at 22:29

0 Answers0