Welcome to Techno Smarter QA, Questions, Answers Community
+3 votes
326 views
 I need your help with my PHP project. My client has given a sql file to me as the database, he wants filter search and fetch the data in pdf format which can printable with different page numbers.    
I am not an expert in PHP so pls can you guide me on how to handle this project.
just attaching that file..and also want to know that how to fetch multiple tables data from one database.. pls
looking for the positive response 
in PHP by (150 points) 2 5
Edit your question and describe the points or code. It will be easy to understand. Kindly make it valuable.
Thanks for the comment, I mean to ask that how to use pdf in PHP coding to get data in pdf view?

1 Answer

+2 votes

Thanks for posting here. I am getting your question. You want to fetch MYSQL database data and generate PDF using PHP and MYSQL database. It's possible that you can get data from the MYSQL database and display it in PDF format. The PDF view will be managed by the PHP code. You will be able to fetch many rows from MySQL database and generate a PDF file. 

How to generate PDF in PHP with MYSQL database

Let's start to generate PDF file using PHP and MYSQL database - 

How to generate PDF file from the MYSQL database using PHP 

First of all, you need to create a database table.

CREATE TABLE `records` (

  `id` int(11) NOT NULL,

  `name` varchar(40) NOT NULL,

  `age` varchar(40) NOT NULL,

  `mobile` varchar(100) NOT NULL

) ENGINE

 

We will fetch and generate data above into PDF file in PHP.  To generate PDF file using PHP , you have to insert some data into MYSQL database table . 

Let's insert some data into MYSQL database table - 

INSERT INTO `records` (`id`, `name`, `age`, `mobile`) VALUES

(1, 'Mohan', '29', '121219212'),

(2, 'Sohan', '23', '21191019281'),

(3, 'Jack', '45', '212121221'),

(4, 'Saim', '25', '2121212123'),

(5, 'Amenda', '32', '21212912912'),

(6, 'John', '21', '121212121');

COMMIT;

All data are inserted well. Now we will fetch that data and generate a PDF file via PHP. 

Let's create confip.php file - 

It will help to connect MYSQL database to the PHP page . We will create a class inside this file . The class will be used to generate PDF file from MYSQL database table . 

 config.php 

<?php 

class Db {

public $databaseHost = "localhost";

public $databaseUser= "root";

    public $databasePassword = "";

public $databaseName = "tutorials";

function query($sql) {

$con = new mysqli($this->databaseHost,$this->databaseUser,$this->databasePassword,$this->databaseName);

    if ($con->connect_error) {

        die("Connection failed: " . $con->connect_error);

    } 

    $result = $con->query($sql);

    if ($result->num_rows > 0) {

      while($row = $result->fetch_assoc()) {

          $resultset[] = $row;

      }

    }

    $con->close();

if(!empty($resultset))

return $resultset;

}

}

?>

Change the connection file credential according to your database . 

Now , we are fetching data and generating pdf file using PHP . 

Create another PHP file

index.php 

<?php

require_once('config.php');

$db = new Db();

$res = $db->query("SELECT id,name,age,mobile FROM records");

$header = $db->query("SELECT UCASE(`COLUMN_NAME`) 

FROM `INFORMATION_SCHEMA`.`COLUMNS` 

WHERE `TABLE_SCHEMA`='tutorials' 

AND `TABLE_NAME`='records'

and `COLUMN_NAME` in ('id','name','age','mobile')");

require_once('fpdf/fpdf.php');

$pdf = new FPDF();

$pdf->AddPage();

$pdf->SetFont('helvetica','B',17);

foreach($header as $heading) {

foreach($heading as $column_heading)

$pdf->Cell(45,12,$column_heading,1);

}

foreach($res as $row) {

$pdf->Ln();

foreach($row as $column)

$pdf->Cell(45,12,$column,1);

}

$pdf->Output();

?>

In the code above, we require fpdf file . 

Download the fpdf file from URL http://www.fpdf.org/ and save in fpdf folder. 

The fpdf is a library. It will help you design a PDF view and download that pdf file using PHP. 

In the code above, we are using cell() function to define the table cells using values. We can set any font family for PDF view using SetFont() . 

These are created functions inside the fpdf file. 

In this way, you can generate a PDF file MYSQL MySQL database using PHP. 

by (4k points) 2 21 52
Thanks for posting best answer.
...